Python中使用Tkinter模块创建GUI程序实例
使用Tkinter模块来创建简单的GUI程序。
Tkinter的Widgets有:Button、Canvas、Checkbutton、Entry、Frame、Label、Listbox、Menu、Menubutton、Message、Radiobutton、Scales、Scrollbar、TEXT、Toplevel等。
例:
#Thisprogramdisplaysanemptywindow. importTkinter defmain(): main_window=Tkinter.Tk() Tkinter.mainloop() main()
例2:
importTkinter classMyGUI: def__init__(self): #Createthemainwindowwidget. self.main_window=Tkinter.Tk() #EntertheTkintermainloop. Tkinter.mainloop() #CreateaninstanceoftheMyGUIclass. my_gui=MyGUI()
例3:
#Theprogramdisplaysalabelwithtext. importTkinter classMyGUI: def__init__(self): self.main_window=Tkinter.Tk() #CreateaLabelwidgetcontainingthetext'Helloworld' self.label=Tkinter.Label(self.main_window,text='HelloWorld!') #CalltheLabelwidget'spackmethod. self.label.pack() #EntertheTkintermainloop. Tkinter.mainloop() #CreateaninstanceoftheMyGUIclass. my_gui=MyGUI()
例4:
importTkinter classMyGUI: def__init__(self): self.main_window=Tkinter.Tk() self.label1=Tkinter.Label(self.main_window,text='HelloWorld!') self.label2=Tkinter.Label(self.main_window,text='ThisismyGUIprogram.') self.label1.pack() self.label2.pack() Tkinter.mainloop() mygui=MyGUI()
例5:
importTkinter classMyGUI: def__init__(self): self.main_window=Tkinter.Tk() self.label1=Tkinter.Label(self.main_window,text='HelloWorld!') self.label2=Tkinter.Label(self.main_window,text='ThisismyGUIprogram.') self.label1.pack(side='left') self.label2.pack(side='left') Tkinter.mainloop() mygui=MyGUI()
例6:
importTkinter classMyGUI: def__init__(self): self.main_window=Tkinter.Tk() self.top_frame=Tkinter.Frame(self.main_window) self.bottom_frame=Tkinter.Frame(self.main_window) self.label1=Tkinter.Label(self.top_frame,text='Winken') self.label2=Tkinter.Label(self.top_frame,text='Blinken') self.label3=Tkinter.Label(self.top_frame,text='Nod') self.label1.pack(side='top') self.label2.pack(side='top') self.label3.pack(side='top') self.label4=Tkinter.Label(self.bottom_frame,text='Winken') self.label5=Tkinter.Label(self.bottom_frame,text='Blinken') self.label6=Tkinter.Label(self.bottom_frame,text='Nod') self.label4.pack(side='left') self.label5.pack(side='left') self.label6.pack(side='left') self.top_frame.pack() self.bottom_frame.pack() Tkinter.mainloop() mygui=MyGUI()
按钮Widget和信息对话框
使用tkMessageBox模块的showinfo函数来显示信息对话框。
例:
#theprogramdemonstratesaButtonwidget. #whentheuserclicksthebutton,aninfodialogboxisdisplayed. importTkinter importtkMessageBox classMyGUI: def__init__(self): self.main_window=Tkinter.Tk() self.my_button=Tkinter.Button(self.main_window,text='Clickme!',command=self.do_something) self.my_button.pack() Tkinter.mainloop() defdo_something(self): tkMessageBox.showinfo('Response','Thanksforclickingthebutton.') mygui=MyGUI()
例2:
importTkinter importtkMessageBox classMyGUI: def__init__(self): self.main_window=Tkinter.Tk() self.my_button=Tkinter.Button(self.main_window,text='Clickme!',command=self.do_something) self.quit_button=Tkinter.Button(self.main_window,text='Quit',command=self.main_window.quit) self.my_button.pack() self.quit_button.pack() Tkinter.mainloop() defdo_something(self): tkMessageBox.showinfo('Response','Thanksforclickingthebutton.') mygui=MyGUI()
用EntryWidget得到输入
EntryWidget是一个矩形区域,用户可输入其中。可使用EntryWidget的get方法取回输入的数据。
例:
importTkinter importtkMessageBox classKiloGUI: def__init__(self): self.main_window=Tkinter.Tk() self.top_frame=Tkinter.Frame(self.main_window) self.bottom_frame=Tkinter.Frame(self.main_window) self.label=Tkinter.Label(self.top_frame,text='Enteradistanceinkilometers:') self.entry=Tkinter.Entry(self.top_frame,width=10) self.button1=Tkinter.Button(self.bottom_frame,text='Convert',command=self.convert) self.button2=Tkinter.Button(self.bottom_frame,text='Quit',command=self.main_window.quit) self.label.pack(side='left') self.entry.pack(side='left') self.button1.pack(side='left') self.button2.pack(side='left') self.top_frame.pack() self.bottom_frame.pack() Tkinter.mainloop() defconvert(self): kilo=float(self.entry.get()) miles=kilo*0.6214 tkMessageBox.showinfo('Result',str(kilo)+'kilometersisequalto'+str(miles)+'miles.') mygui=KiloGUI()
例2:
importTkinter importtkMessageBox classKiloGUI: def__init__(self): self.main_window=Tkinter.Tk() self.top_frame=Tkinter.Frame(self.main_window) self.mid_frame=Tkinter.Frame(self.main_window) self.bottom_frame=Tkinter.Frame(self.main_window) self.label1=Tkinter.Label(self.top_frame,text='Enteradistanceinkilometers:') self.entry=Tkinter.Entry(self.top_frame,width=10) self.button1=Tkinter.Button(self.bottom_frame,text='Convert',command=self.convert) self.button2=Tkinter.Button(self.bottom_frame,text='Quit',command=self.main_window.quit) self.label2=Tkinter.Label(self.mid_frame,text='Convertedtomiles:') self.value=Tkinter.StringVar() self.label3=Tkinter.Label(self.mid_frame,textvariable=self.value) self.label1.pack(side='left') self.entry.pack(side='left') self.button1.pack(side='left') self.button2.pack(side='left') self.label2.pack(side='left') self.label3.pack(side='left') self.top_frame.pack() self.mid_frame.pack() self.bottom_frame.pack() Tkinter.mainloop() defconvert(self): kilo=float(self.entry.get()) miles=kilo*0.6214 self.value.set(miles) mygui=KiloGUI()
Radio按钮和Check按钮
例:
importTkinter importtkMessageBox classMyGUI: def__init__(self): self.main_window=Tkinter.Tk() self.top_frame=Tkinter.Frame(self.main_window) self.bottom_frame=Tkinter.Frame(self.main_window) self.radio_var=Tkinter.IntVar() self.radio_var.set(1) self.rb1=Tkinter.Radiobutton(self.top_frame,text='Option1',variable=self.radio_var,value=1) self.rb2=Tkinter.Radiobutton(self.top_frame,text='Option2',variable=self.radio_var,value=2) self.rb3=Tkinter.Radiobutton(self.top_frame,text='Option3',variable=self.radio_var,value=3) self.rb1.pack() self.rb2.pack() self.rb3.pack() self.ok_button=Tkinter.Button(self.bottom_frame,text='OK',command=self.show_choice) self.quit_button=Tkinter.Button(self.bottom_frame,text='QUIT',command=self.main_window.quit) self.ok_button.pack(side='left') self.quit_button.pack(side='left') self.top_frame.pack() self.bottom_frame.pack() Tkinter.mainloop() defshow_choice(self): tkMessageBox.showinfo('Selection','Youselectedoptioin'+str(self.radio_var.get())) mygui=MyGUI()