欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python中使用Tkinter模块创建GUI程序实例

程序员文章站 2023-11-20 12:25:10
使用tkinter模块来创建简单的gui程序。 tkinter的widgets有:button、canvas、checkbutton、entry、frame、label、...

使用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()