使用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()
用Entry Widget得到输入
Entry Widget是一个矩形区域,用户可输入其中。可使用Entry Widget的get方法取回输入的数据。
例:
复制代码 代码如下:
import Tkinter
import tkMessageBox
class KiloGUI:
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='Enter a distance in kilometers:')
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()
def convert(self):
kilo = float(self.entry.get())
miles = kilo*0.6214
tkMessageBox.showinfo('Result',str(kilo)+' kilometers is equal to '+str(miles)+' miles.')
mygui = KiloGUI()
例2:
复制代码 代码如下:
import Tkinter
import tkMessageBox
class KiloGUI:
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='Enter a distance in kilometers:')
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='Converted to miles:')
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()
def convert(self):
kilo = float(self.entry.get())
miles = kilo*0.6214
self.value.set(miles)
mygui = KiloGUI()
Radio按钮和Check按钮
例:
复制代码 代码如下:
import Tkinter
import tkMessageBox
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.radio_var = Tkinter.IntVar()
self.radio_var.set(1)
self.rb1 = Tkinter.Radiobutton(self.top_frame,text='Option 1',variable=self.radio_var,value=1)
self.rb2 = Tkinter.Radiobutton(self.top_frame,text='Option 2',variable=self.radio_var,value=2)
self.rb3 = Tkinter.Radiobutton(self.top_frame,text='Option 3',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()
def show_choice(self):
tkMessageBox.showinfo('Selection','You selected optioin '+str(self.radio_var.get()))
mygui = MyGUI()
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 《原神》「星路拾忆」网页活动壁纸展示
- 群星.2024-祝你幸福!电影原声带【阿里巴巴】【FLAC分轨】
- 于台烟.2004-执着【乾坤唱片】【WAV+CUE】
- 黄品源.2004-感谢·情人(引进版)【滚石】【WAV+CUE】
- 《最终幻想16》评测:游、戏两难全的系列转折
- 《纸嫁衣》五部打包¥104,《鬼泣5》仅需¥44
- 《崩坏:星穹铁道》造物大赛卷出新高度
- 《王菲 菲常传奇 2CD》[WAV+CUE][950MB]
- 《王菲 空灵独味唱腔 非常传奇 紫水晶2CD》[WAV+CUE][860MB]
- 《车载专业测试王1号 极品音质》[WAV/分轨][980MB]
- 炉石传说的国服回归送什么奖励 炉石传说9.25回归最新消息介绍
- 云顶之弈最强阵容汇总2024 云顶之弈最强阵容搭配最新版本
- S14lol全球总决赛在哪观看 lol全球总决赛观赛直播地址汇总
- 黄思婷2011-感谢天[豪记唱片][WAV+CUE]
- 孙淑媚2013-太阳[台湾首版][WAV+CUE]