使用Tkinter模块来创建简单的GUI程序

使用Tkinter模块来创建简单的GUI程序[python GUI教程]

学了一段时间python,总想用它来做一个gui的界面程序,嘿嘿,主要是为了炫耀一下啊。下面整理了一些python Tkinter模块的小例子,希望大家能够做一个gui程序出来。

使用Tkinter模块来创建简单的GUI程序。
Tkinter的Widgets有:Button、Canvas、Checkbutton、Entry、Frame、Label、Listbox、Menu、Menubutton、Message、Radiobutton、Scales、Scrollbar、TEXT、Toplevel等。

例:
# This program displays an empty window.
import Tkinter
def main():
main_window = Tkinter.Tk()
Tkinter.mainloop()
main()

例2:
import Tkinter
class MyGUI:
def __init__(self):
# Create the main window widget.
self.main_window = Tkinter.Tk()

# Enter the Tkinter main loop.
Tkinter.mainloop()
# Create an instance of the MyGUI class.
my_gui = MyGUI()

例3:
# The program displays a label with text.
import Tkinter
class MyGUI:
def __init__(self):
self.main_window = Tkinter.Tk()
# Create a Label widget containing the text “Hello world”
self.label = Tkinter.Label(self.main_window, text=”Hello World!”)
# Call the Label widget”s pack method.
self.label.pack()
# Enter the Tkinter main loop.
Tkinter.mainloop()
# Create an instance of the MyGUI class.
my_gui = MyGUI()

例4:
import Tkinter
class MyGUI:
def __init__(self):
self.main_window = Tkinter.Tk()
self.label1 = Tkinter.Label(self.main_window,text=”Hello World!”)
self.label2 = Tkinter.Label(self.main_window,text=”This is my GUI program.”)
self.label1.pack()
self.label2.pack()
Tkinter.mainloop()
mygui = MyGUI()

例5:
import Tkinter
class MyGUI:
def __init__(self):
self.main_window = Tkinter.Tk()
self.label1 = Tkinter.Label(self.main_window,text=”Hello World!”)
self.label2 = 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 Tkinter
class MyGUI:
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函数来显示信息对话框。
例:
# the program demonstrates a Button widget.
# when the user clicks the button, an info dialog box is displayed.
import Tkinter
import tkMessageBox
class MyGUI:
def __init__(self):
self.main_window = Tkinter.Tk()
self.my_button = Tkinter.Button(self.main_window, text=”Click me!”,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 Tkinter
import tkMessageBox
class MyGUI:
def __init__(self):
self.main_window = Tkinter.Tk()
self.my_button = Tkinter.Button(self.main_window, text=”Click me!”,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()
def do_something(self):
tkMessageBox.showinfo(“Response”,”Thanks for clicking the button.”)
mygui = MyGUI()

来源:PY学习网:原文地址:https://www.py.cn/article.html

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » 使用Tkinter模块来创建简单的GUI程序