15.图形交互界面之Tkinter-Canvas画布

画布

from tkinter import *
class App(Frame):
    def __init__(self,master = None):
        Frame.__init__(self,master)
        self.pack()

# create the applilcation
myapp = App()

# here are method calls to the window manger class
myapp.master.title("画布")
myapp.master.maxsize(1000, 400)

w =Canvas(myapp,width =400, height =200)
w.pack()

def paint(event):
    x1, y1 = (event.x -1),(event.y - 1)
    x2, y2 = (event.x +1),(event.y + 1)
    w.create_oval(x1,y1,x2,y2,fill = "red")
#绑定功能,画布与鼠标左键绑定 
w.bind("",paint)
Label(myapp,text ="按住鼠标左键并移动,\
                 开始绘制你的理想蓝图吧!.......").pack(side = BOTTOM)

mainloop()

画布之列表

from tkinter import *           #导入tkinter库
root = Tk() 
#创建窗口对象背景
root.title('画布')
#创建两个列表

li = ['c','python','php','html','sql','java']
movie = ['CSS','JQuery','Bootstrap']

#创建两个列表组件

listb = Listbox(root)                     
listb2 = Listbox(root)


for item in li:                            #第一个小部件插入数据
    listb.insert(0, item)

for item in movie:                          #第二个小部件插入数据
    listb2.insert(0, item)


listb.pack()                                 #将小部件放置到主窗口中
listb2.pack()

root.mainloop()                               #进入消息循环
15.图形交互界面之Tkinter-Canvas画布_第1张图片
image.png

画布之长方形及线

from tkinter import *
class App(Frame):
    def __init__(self,master = None):
        Frame.__init__(self,master)
        self.pack()

# create the applilcation
myapp = App()


#
# here are method calls to the window manger class
#
myapp.master.title("画布")
myapp.master.maxsize(1000,400)


w = Canvas(myapp,width =200,height =200)  #创建一个Canvas窗口
                                #将创建的Canvas小窗口放置在主窗口中


w.pack()  
w.create_line(0,50,200,50,fill ="yellow")
w.create_line(100,0,100,100,fill ="red",dash =(4,4))
w.create_rectangle(50,25,150,75,fill="blue")

mainloop()
15.图形交互界面之Tkinter-Canvas画布_第2张图片
image.png

画布之椭圆字幕butten组件

    from tkinter import *
    class App(Frame):
    def __init__(self,master = None):
        Frame.__init__(self,master)
        self.pack()



# create the applilcation
myapp = App()


#
# here are method calls to the window manger class
#
myapp.master.title("画布")
myapp.master.maxsize(1000, 400)    #画布大小设置



w = Canvas(myapp,width =200,height =200)  #Canvas窗口设置
w.pack()  #显示Canvas窗口

#绘制直线及长方形
line1 = w.create_line(0,50,200,50,fill ="yellow")
line2 = w.create_line(100,0,100,100,fill ="red",dash =(4,4))
rect1 = w.create_rectangle(50,25,150,75,fill="blue")

#重建line1的坐标
w.coords(line1,0,25,200,25)
#修改rect1的填充颜色
w.itemconfig(rect1,fill ="red")
#删除line2
w.delete(line2)
#添加按钮,删除所有
Button(myapp,text="删除全部",command =(lambda x =ALL:w.delete(x))).pack() #创建butten设置点动删除所有


w.create_line(0,0,200,100,fill ="green",width =3)
w.create_line(200,0,0,100,fill ="green",width =3)
#w.create_rectangle(40,20,160,80,fill="green")
#w.create_rectangle(60,40,140,65,fill="yellow")

w.create_rectangle(40,20,160,80,dash =(4,4)) #创建长方形坐标
w.create_oval(40,20,160,80,fill ="pink")   #创建椭圆,设置颜色为pink
w.create_text(100,50,text="I Love pig",fill = "yellow") #创建文字描述,填充色可修改


#w.create_text(100,50,text="DonkeyJason")

mainloop()
15.图形交互界面之Tkinter-Canvas画布_第3张图片
image.png

画布之五角星

from tkinter import *
import math as m
class App(Frame):
    def __init__(self,master = None):
        Frame.__init__(self,master)
        self.pack()



# create the applilcation
myapp = App()


#
# here are method calls to the window manger class
#
myapp.master.title("五角星")
myapp.master.maxsize(1000, 400)



w = Canvas(myapp, width =200, height=100,)
w.pack()


center_x =100
center_y =50
r =50

points = [
    #左上点
    center_x -int(r * m.sin(2 * m.pi / 5)),
    center_y -int(r * m.cos(2 * m.pi / 5)),
    #右上点
    center_x + int(r * m.sin(2 * m.pi / 5)),
    center_y - int(r * m.cos(2 * m.pi / 5)),
    #左下点
    center_x - int(r * m.sin(m.pi / 5)),
    center_y + int(r * m.cos(m.pi / 5)),

    #顶点
    center_x ,
    center_y -r,
    #右下点
    center_x + int(r * m.sin(m.pi / 5)),
    center_y + int(r * m.cos(m.pi / 5))

    ]

w.create_polygon(points,outline ="green", fill ="green")
w.create_text(100,50,text="Donkey Jason",fill = "red")
mainloop()
15.图形交互界面之Tkinter-Canvas画布_第4张图片
image.png
  • 修改参数
    center_x =200
    center_y =100
    r =100


    15.图形交互界面之Tkinter-Canvas画布_第5张图片
    image.png

你可能感兴趣的:(15.图形交互界面之Tkinter-Canvas画布)