import Tkinter
root=Tkinter.Tk()
label=Tkinter.Label(root,text='hello ,python')
label.pack() #将LABEL组件添加到底框上
button1=Tkinter.Button(root,text='BUTTON1')
button1.pack(side=Tkinter.LEFT)
button2=Tkinter.Button(root,text='BUTTON2')
button2.pack(side=Tkinter.RIGHT)
root.mainloop()
#默认 pack()将向下添加组件。
import Tkinter
root=Tkinter.Tk()
button1=Tkinter.Button(root,anchor=Tkinter.E,height=5,width=40,text='BUTTON1')
button1.pack()
button2=Tkinter.Button(root,text='BUTTON2',bg='blue')
button2.pack()
button3=Tkinter.Button(root,text='BUTTOON3',fg='red')
button3.pack()
button4=Tkinter.Button(root,text='BUTTOON4',state=Tkinter.DISABLED)
button4.pack()
root.mainloop()
import Tkinter
root=Tkinter.Tk()
r=Tkinter.StringVar()
r.set('1')
radio=Tkinter.Radiobutton(root,variable=r,value='1',text='Radio1')
radio.pack()
radio=Tkinter.Radiobutton(root,variable=r,value='2',text='Radio2')
radio.pack()
radio=Tkinter.Radiobutton(root,variable=r,value='3',text='Radio3')
radio.pack()
radio=Tkinter.Radiobutton(root,variable=r,value='4',text='Radio4')
radio.pack()
radio=Tkinter.Radiobutton(root,variable=r,value='5',text='Radio5')
radio.pack()
c=Tkinter.IntVar()
c.set(1)
check=Tkinter.Checkbutton(root,text='check button',variable=c,onvalue=1,offvalue=2)
check.pack()
root.mainloop()
print c.get()
print r.get()
import Tkinter
class MyButton:
def __init__(self,root,canvas,label,type):
self.root=root
self.canvas=canvas
self.label=label
if type==0:
button=Tkinter.Button(root,text='DrawLine',command=self.DrawLine)
elif type==1:
button=Tkinter.Button(root,text='DrawArc',command=self.DrawArc)
elif type==2:
button=Tkinter.Button(root,text='DrawRect',command=self.DrawRect)
else:
button=Tkinter.Button(root,text='DrawOval',command=self.DrawOval)
button.pack(side='left')
def DrawLine(self):
self.label.text.set('Draw Line')
self.canvas.SetStatus(0)
def DrawArc(self):
self.label.text.set('Draw Arc')
self.canvas.SetStatus(1)
def DrawRect(self):
self.label.text.set('Draw Rect')
self.canvas.SetStatus(2)
def DrawOval(self):
self.label.text.set('Draw Oval')
self.canvas.SetStatus(3)
class MyCanvas:
def __init__(self,root):
self.status=0
self.draw=0
self.root=root
self.canvas=Tkinter.Canvas(root,bg='white',width=600,height=480)
self.canvas.pack()
self.canvas.bind('<ButtonRelease-1>',self.Draw)
self.canvas.bind('<Button-2>',self.Exit)
self.canvas.bind('<Button-3>',self.Del)
self.canvas.bind_all('<Delete>',self.Del)
self.canvas.bind_all('<KeyPress-d>',self.Del)
self.canvas.bind_all('<KeyPress-e>',self.Exit)
def Draw(self,event):
if self.draw==0:
self.x=event.x
self.y=event.y
self.draw=1
else:
if self.status==0:
self.canvas.create_line(self.x,self.y,event.x,event.y)
self.draw=0
elif self.status==1:
self.canvas.create_arc(self.x,self.y,event.x,event.y)
self.draw=0
elif self.status==2:
self.canvas.create_rectangle(self.x,self.y,event.x,event.y)
self.draw=0
else:
self.canvas.create_oval(self.x,self.y,event.x,event.y)
self.draw=0
def Del(self,event):
items=self.canvas.find_all()
for item in items:
self.canvas.delete(item)
def Exit(self,event):
self.root.quit()
def SetStatus(self,status):
self.status=status
class MyLabel:
def __init__(self,root):
self.root=root
self.canvas=canvas
self.text=Tkinter.StringVar()
self.text.set('Draw Line')
self.label=Tkinter.Label(root,textvariable=self.text,fg='red',width=50)
self.label.pack(side='left')
root=Tkinter.Tk()
canvas=MyCanvas(root)
label=MyLabel(root)
MyButton(root,canvas,label,0)
MyButton(root,canvas,label,1)
MyButton(root,canvas,label,2)
MyButton(root,canvas,label,3)
root.mainloop()