GUI
2020-05-28 本文已影响0人
Silmarillion123
from tkinter import *
class WidgetDemo:
def __init__(self):
window=Tk()
window.title("Widget Demo")
frame1=Frame(window)
frame1.pack()
self.v1=IntVar()#int类型的变量
cbtBold=Checkbutton(frame1,text="Bold",variable=self.v1,command=self.processCheckButton)#checkbutton可在值之间切换
self.v2=IntVar()
rbRed=Radiobutton(frame1,text="red",bg="red",variable=self.v2,value=1,command=self.processRadiusButton)#radiobutton单击单选变量
rbyellow=Radiobutton(frame1,text="yellow",bg="yellow",variable=self.v2,value=2,command=self.processRadiusButton)
cbtBold.grid(row=1,column=1)#对几个按钮进行排版
rbRed.grid(row=1,column=2)
rbyellow.grid(row=1,column=3)
frame2=Frame(window)
frame2.pack()
label=Label(frame2,text="enter your name: ")
self.name=StringVar()
entryname =Entry(frame2,textvariable=self.name)
btGetname=Button(frame2,text="get name",command=self.processButton)
message=Message(frame2,text=" it is a widgets demo")#label可显示图像和文字,message只能显示文字
label.grid(row=1,column=1)
entryname.grid(row=1,column=2)
btGetname.grid(row=1,column=3)
message.grid(row=1,column=4)
text=Text(window)
text.pack()
text.insert(END,"A\n")#从结尾处插入
text.insert(END,"B\n")
text.insert(END,"C\n")
window.mainloop()
def processCheckButton(self):
print("check button is "+("checked"if self.v1.get()==1 else "unchecked"))
def processRadiusButton(self):
print(("red" if self.v2.get()==1 else "yellow")+" is selected")
def processButton(self):
print("your name is "+self.name.get())
WidgetDemo()
#只有window下属一个等级的要pack,其他不用
效果图
from tkinter import *
class canvasDemo:
def __init__(self):
window=Tk()
window.title("canvas")
self.canvas=Canvas(window,width=200,height=100,bg="white")
self.canvas.pack()
frame=Frame(window)
frame.pack()
drawrectangle=Button(frame,text="Rectangle",command=self.displayrect)
drawoval=Button(frame,text="Oval",command=self.displayOval)
drawarc=Button(frame,text="Arc",command=self.displayArc)
drawpolygon=Button(frame,text="Polygon",command=self.diaplayPolygon)
drawline=Button(frame,text="Line",command=self.displayLine)
drawstring=Button(frame,text="string",command=self.displayString)
clearup=Button(frame,text="clear",command=self.cleanup)
drawrectangle.grid(row=1,column=1)
drawoval.grid(row=1,column=2)
drawarc.grid(row=1,column=3)
drawline.grid(row=1,column=4)
drawstring.grid(row=1,column=5)
drawpolygon.grid(row=1,column=6)
clearup.grid(row=1,column=7)
window.mainloop()
def displayrect(self):
self.canvas.create_rectangle(10,10,190,90,tags="rect")
def displayOval(self):
self.canvas.create_oval(10,10,190,90,fill="red",tags="oval")
def displayArc(self):
self.canvas.create_arc(10,10,190,90,start=0,extent=90,width=8,fill="red",tags="arc")
def diaplayPolygon(self):
self.canvas.create_polygon(10,10,190,90,30,50,tags="polygon")
def displayString(self):
self.canvas.create_text(60,40,text="hi",font="twenty 10 bold underline",tags="string")
def displayLine(self):
self.canvas.create_line(10,10,190,90,fill="red",tags="line")
self.canvas.create_line(10,90,190,10,width=9,arrow="last",activefill="blue",tags="line")
def cleanup(self):
self.canvas.delete("rect","line","string","polygon","arc","oval")
canvasDemo()
image.png