在Tkinter, 帆布类用于在Canvas类下定义的某些功能的帮助下创建不同的形状。 Canvas类创建的任何形状都需要一个画布, 因此在创建任何形状之前, 需要一个Canvas对象, 并且必须将其打包到主窗口中。
画布的形状方法:Canvas.create_oval(x1, y1, x2, y2, options =…):用于创建椭圆形, 派斯和弦。我们正在使用一个类来展示有助于创建不同形状的函数的工作方式。
Canvas.create_rectangle(x1, y1, x2, y2, options =…):用于创建矩形和正方形。
Canvas.create_arc(x1, y1, x2, y2, options =…)用于创建圆弧。
Canvas.create_polygon(coordinates, options =…)用于创建任何有效的形状。
类参数–
使用的数据成员:master,以下是Python代码–
canvas使用的成员函数:create()方法
使用的小部件:Canvas
Tkinter使用的方法:Canvas.create_oval()
Canvas.create_rectangle()
Canvas.create_arc()
Canvas.create_polygon()
pack()
title()
geometry()
# Imports each and every method and class
# of module tkinter and tkinter.ttk
from tkinter import * from tkinter.ttk import * class Shape:
def __init__( self , master = None ):
self .master = master# Calls create method of class Shape
self .create()def create( self ):# Creates a object of class canvas
# with the help of this we can create different shapes
self .canvas = Canvas( self .master)# Creates a circle of diameter 80
self .canvas.create_oval( 10 , 10 , 80 , 80 , outline = "black" , fill = "white" , width = 2 )# Creates an ellipse with horizontal diameter
# of 210 and vertical diameter of 80
self .canvas.create_oval( 110 , 10 , 210 , 80 , outline = "red" , fill = "green" , width = 2 )# Creates a rectangle of 50x60 (heightxwidth)
self .canvas.create_rectangle( 230 , 10 , 290 , 60 , outline = "black" , fill = "blue" , width = 2 )# Creates an arc of 210 deg
self .canvas.create_arc( 30 , 200 , 90 , 100 , start = 0 , extent = 210 , outline = "green" , fill = "red" , width = 2 )points = [ 150 , 100 , 200 , 120 , 240 , 180 , 210 , 200 , 150 , 150 , 100 , 200 ]# Creates a polygon
self .canvas.create_polygon(points, outline = "blue" , fill = "orange" , width = 2 )
# Pack the canvas to the main window and make it expandable
self .canvas.pack(fill = BOTH, expand = 1 )if __name__ = = "__main__" :# object of class Tk, resposible for creating
# a tkinter toplevel window
master = Tk()
shape = Shape(master)# Sets the title to Shapes
master.title( "Shapes" )# Sets the geometry and position
# of window on the screen
master.geometry( "330x220 + 300 + 300" )# Infnite loop breaks only by interrupt
mainloop()
输出如下:
文章图片
【Python Tkinter使用Canvas类创建不同的形状】首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。
推荐阅读
- Python使用Tkinter进行消息编码和解码
- Python Tkinter使用Canvas类创建不同类型的线
- Python Tkinter Entry小部件用法实例
- Python Tkinter无框窗口用法示例
- 二进制方式部署k8s集群(超详细)
- #yyds干货盘点# 如何挑选合适的 Kubernetes 存储插件((09))
- Linux之watch命令
- #yyds干货盘点#使用线程安全型双向链表实现简单 LRU Cache 模拟
- Rock Pi开发笔记(入手Rock Pi 4B plus(基于瑞星微RK3399)板子并制作系统运行)