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


Posted in Python onJanuary 14, 2015

使用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 相关文章推荐
python 自动提交和抓取网页
Jul 13 Python
实例讲解Python编程中@property装饰器的用法
Jun 20 Python
windows下python之mysqldb模块安装方法
Sep 07 Python
opencv python 傅里叶变换的使用
Jul 21 Python
python使用response.read()接收json数据的实例
Dec 19 Python
Python多进程方式抓取基金网站内容的方法分析
Jun 03 Python
Python面向对象封装操作案例详解
Dec 31 Python
通过python实现windows桌面截图代码实例
Jan 17 Python
python next()和iter()函数原理解析
Feb 07 Python
tensorflow实现读取模型中保存的值 tf.train.NewCheckpointReader
Feb 10 Python
如何将PySpark导入Python的放实现(2种)
Apr 26 Python
python如何控制进程或者线程的个数
Oct 16 Python
更改Python命令行交互提示符的方法
Jan 14 #Python
Python的迭代器和生成器使用实例
Jan 14 #Python
python实现带验证码网站的自动登陆实现代码
Jan 12 #Python
Python三元运算实现方法
Jan 12 #Python
Python中的True,False条件判断实例分析
Jan 12 #Python
Python基类函数的重载与调用实例分析
Jan 12 #Python
Python类的专用方法实例分析
Jan 09 #Python
You might like
php excel类 phpExcel使用方法介绍
2010/08/21 PHP
浅析php插件 Simple HTML DOM 用DOM方式处理HTML
2013/07/01 PHP
PHP获取当前url的具体方法全面解析
2013/11/26 PHP
php中实现可以返回多个值的函数实例
2015/03/21 PHP
ThinkPHP V2.2说明文档没有说明的那些事实例小结
2015/07/01 PHP
PHP strip_tags() 去字符串中的 HTML、XML 以及 PHP 标签的函数
2016/05/22 PHP
Laravel中的Auth模块详解
2017/08/17 PHP
window.location.hash 使用说明
2010/11/08 Javascript
关于javascript event flow 的一个bug详解
2013/09/17 Javascript
简单的Jquery遮罩层代码实例
2013/11/14 Javascript
JS获取URL中参数值(QueryString)的4种方法分享
2014/04/12 Javascript
24款热门实用的jQuery插件推荐
2014/12/24 Javascript
深入解析JavaScript中的数字对象与字符串对象
2015/10/21 Javascript
JavaScript笔记之数据属性和存储器属性
2016/03/31 Javascript
React中使用collections时key的重要性详解
2017/08/07 Javascript
JS设计模式之命令模式概念与用法分析
2018/02/06 Javascript
Vue 实现列表动态添加和删除的两种方法小结
2018/09/07 Javascript
React中this丢失的四种解决方法
2019/03/12 Javascript
使用localStorage替代cookie做本地存储
2019/09/25 Javascript
JavaScript函数Call、Apply原理实例解析
2020/02/17 Javascript
vue element-ui中table合计指定列求和实例
2020/11/02 Javascript
重命名批处理python脚本
2013/04/05 Python
Python中内建函数的简单用法说明
2016/05/05 Python
Django时区详解
2019/07/24 Python
Python模块汇总(常用第三方库)
2019/10/07 Python
Win10下python 2.7与python 3.7双环境安装教程图解
2019/10/12 Python
Python双链表原理与实现方法详解
2020/02/22 Python
python 元组的使用方法
2020/06/09 Python
django 实现后台从富文本提取纯文本
2020/07/02 Python
python--shutil移动文件到另一个路径的操作
2020/07/13 Python
渗透攻击的测试步骤
2014/06/07 面试题
微观物理专业自荐信
2014/01/26 职场文书
人力资源管理专业自荐书范文
2014/02/10 职场文书
毕业生如何写自荐信
2014/03/26 职场文书
2015年置业顾问工作总结
2015/04/07 职场文书
2015年社区矫正工作总结
2015/04/21 职场文书