Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(Canvas)

导读
动态演示调用python库的tkinter带你进入GUI世界(Canvas)

目录

tkinter应用案例

1、tkinter应用案例:在Canvas画布上绘制线条、矩形等

2、tkinter应用案例:将Canvas画布上绘制线条、矩形等进行更改、删除等操作

3、tkinter应用案例:将Canvas画布上绘制对角线、矩形、添加文本内容

4、tkinter应用案例:在Canvas画布上绘制对角线、椭圆形、添加文本内容

5、tkinter应用案例:Canvas画布上绘制五角星

6、tkinter应用案例:Canvas画布上随意绘画


tkinter应用案例

1、tkinter应用案例:在Canvas画布上绘制线条、矩形等

python画图去除白线 python画布删除图形_python库


#tkinter应用案例:在Canvas画布上绘制线条、矩形等
from tkinter import *
 
root = Tk()
root.title("Jason niu工作室")
theLabel=tk.Label(root,text="进入GUI世界,请开始你的表演!n(Canvas画布可以让你尽情挥洒)")  
theLabel.pack()

w = Canvas(root,width=400,height=200,background="white")  
w.pack()

w.create_line(0,100,400,100,fill="Green")         
w.create_line(200,0,200,200,fill="red",dash=(4,4))   
w.create_rectangle(100,50,300,150,fill="blue")      

mainloop()


2、tkinter应用案例:将Canvas画布上绘制线条、矩形等进行更改、删除等操作


python画图去除白线 python画布删除图形_python库_02

#tkinter应用案例:将Canvas画布上绘制线条、矩形等进行更改、删除等操作
from tkinter import *
 
root = Tk()
root.title("Jason niu工作室")
theLabel=tk.Label(root,text="进入GUI世界,请开始你的表演!n(更改、删除Canvas画布上的内容)")  
theLabel.pack()

w = Canvas(root,width=200,height=100,background="white")
w.pack()

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")

#修改的三个方法coords、itemconfig、delete(move)
w.coords(line1,0,25,200,25)     
w.itemconfig(rect1,fill = "red") 
w.delete(line2)                 

Button(root,text="删除全部",command=(lambda x=ALL:w.delete(x))).pack()  

mainloop()


3、tkinter应用案例:将Canvas画布上绘制对角线、矩形、添加文本内容

python画图去除白线 python画布删除图形_canvas清除上一次画的_03




#tkinter应用案例:将Canvas画布上绘制对角线、矩形、添加文本内容
from tkinter import *

root = Tk()
root.title("Jason niu工作室")
theLabel=tk.Label(root,text="进入GUI世界,请开始你的表演!n(更改、删除Canvas画布上的内容)")  
theLabel.pack()

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

w.create_line(0,0,400,200,fill="green",width=3) 
w.create_line(400,0,0,200,fill="green",width=3) 
w.create_rectangle(80,40,320,160,fill="green")   
w.create_rectangle(130,70,270,130,fill="yellow")

w.create_text(200,100,text="Jason niu工作室")  

mainloop()


4、tkinter应用案例:在Canvas画布上绘制对角线、椭圆形、添加文本内容


python画图去除白线 python画布删除图形_canvas画布 带箭头的虚线_04


#tkinter应用案例:在Canvas画布上绘制对角线、椭圆形、添加文本内容
from tkinter import *

root = Tk()
root.title("Jason niu工作室")
theLabel=tk.Label(root,text="进入GUI世界,请开始你的表演!n(在Canvas画布上绘制对角线、椭圆形、添加文本内容)")  
theLabel.pack()

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

w.create_rectangle(40,20,160,80,dash=(4,4)) 
w.create_oval(40,20,160,80,fill="blue")      
w.create_text(100,50,text="Jason niu工作室")

mainloop()


5、tkinter应用案例:Canvas画布上绘制五角星


python画图去除白线 python画布删除图形_python库_05


#tkinter应用案例:Canvas画布上绘制五角星
from tkinter import *
import math as m 
   
root = Tk()
root.title("Jason niu工作室")
theLabel=tk.Label(root,text="进入GUI世界,请开始你的表演!n(我就是这么任性,绘制五角星!)")  
theLabel.pack() 
 
w = Canvas(root,width=200,height=100)
w.pack()
 
center_x = 100 
center_y = 50
r=50
 
 
point = [ 
    # 左上点
    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(point,outline="blue",fill="red") 
 
mainloop()


6、tkinter应用案例:Canvas画布上随意绘画


python画图去除白线 python画布删除图形_canvas清除上一次画的_06


#tkinter应用案例:Canvas画布上随意绘画
from tkinter import *
import math as m
   
root = Tk()
root.title("Jason niu工作室")
theLabel=tk.Label(root,text="进入GUI世界,请开始你的表演!n(我是神笔马良,想怎么画就怎么画!)")  
theLabel.pack() 

w = Canvas(root,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="blue") 

w.bind("<B1-Motion>",paint)  #将画布与鼠标左键绑定,绑定方法是paint方法

Label(root,text="把鼠标左键当作你的画笔,绘制你梦想的世界吧......").pack(side=BOTTOM)

mainloop()