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的MongoDB模块PyMongo操作方法集锦
Jan 05 Python
详解Python读取配置文件模块ConfigParser
May 11 Python
Python爬虫框架Scrapy常用命令总结
Jul 26 Python
Python实现多线程的两种方式分析
Aug 29 Python
python实现任意位置文件分割的实例
Dec 14 Python
python 爬虫百度地图的信息界面的实现方法
Oct 27 Python
TensorFlow:将ckpt文件固化成pb文件教程
Feb 11 Python
pycharm使用技巧之自动调整代码格式总结
Nov 04 Python
matplotlib实现数据实时刷新的示例代码
Jan 05 Python
golang特有程序结构入门教程
Jun 02 Python
Python如何导出导入所有依赖包详解
Jun 08 Python
Python turtle实现贪吃蛇游戏
Jun 18 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中inlcude()性能对比详解
2012/09/16 PHP
PHP实现微信网页授权开发教程
2016/01/19 PHP
PHP自定义图片缩放函数实现等比例不失真缩放的方法
2016/08/19 PHP
Windows平台实现PHP连接SQL Server2008的方法
2017/07/26 PHP
Javascript string 扩展库代码
2010/04/09 Javascript
更换select下拉菜单背景样式的实现代码
2011/12/20 Javascript
js截取小数点后几位的写法
2013/11/14 Javascript
location.href用法总结(最主要的)
2013/12/27 Javascript
ECMAScript6函数剩余参数(Rest Parameters)
2015/06/12 Javascript
jQuery实现可编辑的表格实例讲解(2)
2015/09/17 Javascript
jQuery实现动态生成表格并为行绑定单击变色动作的方法
2017/04/17 jQuery
Vue组件开发之LeanCloud带图形校验码的短信发送功能
2017/11/07 Javascript
jquery 验证用户名是否重复代码实例
2019/05/14 jQuery
Vue2.X和Vue3.0数据响应原理变化的区别
2019/11/07 Javascript
Vue使用路由钩子拦截器beforeEach和afterEach监听路由
2020/11/16 Javascript
Python使用Scrapy爬取妹子图
2015/05/28 Python
PyCharm代码整体缩进,反向缩进的方法
2018/06/25 Python
对Python使用mfcc的两种方式详解
2019/01/09 Python
Python实现定期检查源目录与备份目录的差异并进行备份功能示例
2019/02/27 Python
django模型动态修改参数,增加 filter 字段的方式
2020/03/16 Python
python上传时包含boundary时的解决方法
2020/04/08 Python
python 等差数列末项计算方式
2020/05/03 Python
Python中logger日志模块详解
2020/08/04 Python
CSS3的column-fill属性对齐列内容高度的用法详解
2016/07/01 HTML / CSS
美国经典刺绣和字母儿童服装特卖:Smocked Auctions
2018/07/16 全球购物
国际商务英语专业求职信
2014/07/08 职场文书
领导干部作风整顿剖析材料
2014/10/11 职场文书
2014年财务工作总结范文
2014/11/11 职场文书
给学校的建议书400字
2015/09/14 职场文书
聘任协议书(挂靠)
2015/09/21 职场文书
Ajax实现三级联动效果
2021/10/05 Javascript
mysql5.7的安装及Navicate长久免费使用的实现过程
2021/11/17 MySQL
SQLServer RANK() 排名函数的使用
2022/03/23 SQL Server
APP界面设计技巧和注意事项
2022/04/29 杂记
Windows server 2012 NTP时间同步的实现
2022/06/25 Servers
centos环境下nginx高可用集群的搭建指南
2022/07/23 Servers