2022年Python中使用Tkinter模块创建GUI程序实例 .pdf





《2022年Python中使用Tkinter模块创建GUI程序实例 .pdf》由会员分享,可在线阅读,更多相关《2022年Python中使用Tkinter模块创建GUI程序实例 .pdf(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Python 中使用 Tkinter 模块创建 GUI 程序实例这篇文章主要介绍了Python 中使用 Tkinter 模块创建GUI 程序实例,本文给出了创建窗口、文本框、按钮等实例,需要的朋友可以参考下使用 Tkinter 模块来创建简单的GUI 程序。Tkinter 的 Widgets 有:Button、Canvas、Checkbutton、Entry、Frame、Label、Listbox、Menu、Menubutton、Message、Radiobutton、Scales、Scrollbar、TEXT、Toplevel 等。例:复制代码代码如下:#Thisprogram displa
2、ys an empty window.import Tkinterdef main():main_window=Tkinter.Tk()Tkinter.mainloop()main()例 2:复制代码代码如下:import TkinterclassMyGUI:def _init_(self):#Createthe main window widget.self.main_window=Tkinter.Tk()#Enter the Tkinter main loop.Tkinter.mainloop()#Createan instance of the MyGUI class.my_gui=My
3、GUI()例 3:名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 7 页 -复制代码代码如下:#The program displays a label with text.import TkinterclassMyGUI:def _init_(self):self.main_window=Tkinter.Tk()#Createa Label widget containing the text Hello worldself.label=Tkinter.Label(self.main_window,text=Hello World!)#Callthe Label widgets
4、 pack method.self.label.pack()#Enter the Tkinter main loop.Tkinter.mainloop()#Createan instance of the MyGUI class.my_gui=MyGUI()例 4:复制代码代码如下:import TkinterclassMyGUI:def _init_(self):self.main_window=Tkinter.Tk()self.label1=Tkinter.Label(self.main_window,text=HelloWorld!)self.label2=Tkinter.Label(s
5、elf.main_window,text=This is my GUI program.)self.label1.pack()self.label2.pack()Tkinter.mainloop()mygui=MyGUI()例 5:复制代码代码如下:import TkinterclassMyGUI:def _init_(self):self.main_window=Tkinter.Tk()名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 7 页 -self.label1=Tkinter.Label(self.main_window,text=HelloWorld!)self.lab
6、el2=Tkinter.Label(self.main_window,text=This is my GUI program.)self.label1.pack(side=left)self.label2.pack(side=left)Tkinter.mainloop()mygui=MyGUI()例 6:复制代码代码如下:import TkinterclassMyGUI:def _init_(self):self.main_window=Tkinter.Tk()self.top_frame=Tkinter.Frame(self.main_window)self.bottom_frame=Tki
7、nter.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
8、_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 和信息对
9、话框使用 tkMessageBox 模块的 showinfo 函数来显示信息对话框。名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 7 页 -例:复制代码代码如下:#the program demonstrates a Button widget.#when the user clicks the button,an info dialog box is displayed.import Tkinterimport tkMessageBoxclassMyGUI:def _init_(self):self.main_window=Tkinter.Tk()self.my_button
10、=Tkinter.Button(self.main_window,text=Clickme!,command=self.do_something)self.my_button.pack()Tkinter.mainloop()def do_something(self):tkMessageBox.showinfo(Response,Thanks for clicking the button.)mygui=MyGUI()例 2:复制代码代码如下:import Tkinterimport tkMessageBoxclassMyGUI:def _init_(self):self.main_windo
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年Python中使用Tkinter模块创建GUI程序实例 2022 Python 使用 Tkinter 模块 创建 GUI 程序 实例

限制150内