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

 更新时间:2015年01月14日 09:21:16   投稿:junjie  
这篇文章主要介绍了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()

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

相关文章

  • python sqlite3 判断cursor的结果是否为空的案例

    python sqlite3 判断cursor的结果是否为空的案例

    这篇文章主要介绍了python sqlite3 判断cursor的结果是否为空的案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • 浅谈django的render函数的参数问题

    浅谈django的render函数的参数问题

    今天小编就为大家分享一篇浅谈django的render函数的参数问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-10-10
  • 详解python编程slice与indices函数用法示例

    详解python编程slice与indices函数用法示例

    这篇文章主要介绍了详解python编程中slice与indices使用示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2021-09-09
  • Python输入整数进行排序方式

    Python输入整数进行排序方式

    这篇文章主要介绍了Python输入整数进行排序方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • Python用函数思想完成哥德巴赫猜想代码分析

    Python用函数思想完成哥德巴赫猜想代码分析

    在本篇文章里小编给大家整理的是一篇关于Python用函数思想完成哥德巴赫猜想代码分析,有兴趣的朋友们可以参考下。
    2021-06-06
  • python基础之//、/与%的区别详解

    python基础之//、/与%的区别详解

    在学习Python或者使用Python进行工作的时候,大家应该都看到过“/”和“//”,下面这篇文章主要给大家介绍了关于python基础之//、/与%区别的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-06-06
  • 使用Python实现遗传算法的详细步骤

    使用Python实现遗传算法的详细步骤

    遗传算法是模仿自然界生物进化机制发展起来的随机全局搜索和优化方法,它借鉴了达尔文的进化论和孟德尔的遗传学说,其本质是一种高效、并行、全局搜索的方法,本文给大家介绍了使用Python实现遗传算法的详细步骤,需要的朋友可以参考下
    2023-11-11
  • 用pickle存储Python的原生对象方法

    用pickle存储Python的原生对象方法

    下面小编就为大家带来一篇用pickle存储Python的原生对象方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • Python图像处理库PIL的ImageDraw模块介绍详解

    Python图像处理库PIL的ImageDraw模块介绍详解

    这篇文章主要介绍了Python图像处理库PIL的ImageDraw模块介绍详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • Python基于os.environ从windows获取环境变量

    Python基于os.environ从windows获取环境变量

    这篇文章主要介绍了Python基于os.environ从windows获取环境变量,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06

最新评论