与radiobutton不同的是,checkbutton可以多选
'''
主题:tk学习
内容:Checkbutton 勾选项
时间:2021/10/19
'''
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('200x200')
l = tk.Label(window,bg='yellow',width=20,text='empty')
l.pack()
var1 = tk.IntVar()
var2 = tk.IntVar()
def print_selection():
if(var1.get()==1 and var2.get()==1 ):
l.config(text='you hava selected both')
elif(var1.get()==1 and var2.get()==0):
l.config(text='you hava selected python')
elif(var1.get()==0 and var2.get()==1):
l.config(text='you hava selected c++')
else:
l.config(text='you don\'t hava selected')
c1 = tk.Checkbutton(window,text='python',
variable=var1,
onvalue=1,offvalue=0,
command=print_selection
)
c2 = tk.Checkbutton(window,text='c++',
variable=var2,
onvalue=1,offvalue=0,
command=print_selection
)
c1.pack()
c2.pack()
window.mainloop()
'''
主题:tk学习
内容:Canvas 画布
时间:2021/10/19
'''
import tkinter as tk
from tkinter.constants import ANCHOR
from typing import Sized
window = tk.Tk()
window.title('my window')
window.geometry('600x600')
c = tk.Canvas(window,bg='blue',height=800,width=800)
image_file = tk.PhotoImage(file='user.gif')
image = c.create_image(10,10,
anchor='nw',
image=image_file
)
x0,y0,x1,y1 = 110,110,150,150
line = c.create_line(x0,y0,x1,y1)
oval = c.create_oval(x0,y0,x1,y1,fill='red')
arc = c.create_arc(x0+50,y0+50,x1+100,y1+100,fill='green',start=0,extent=180)
rect = c.create_rectangle(10,10,100,100,fill='yellow')
def moveit():
c.move(rect,0,10)
b = tk.Button(window,text='move',command=moveit).pack()
c.pack()
window.mainloop()
|