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批量提交沙箱问题实例
Oct 08 Python
在Django中同时使用多个配置文件的方法
Jul 22 Python
使用FastCGI部署Python的Django应用的教程
Jul 22 Python
Python脚本暴力破解栅栏密码
Oct 19 Python
深入解析Python的Tornado框架中内置的模板引擎
Jul 11 Python
OpenCV+python手势识别框架和实例讲解
Aug 03 Python
对Python中画图时候的线类型详解
Jul 07 Python
简单了解Django ContentType内置组件
Jul 23 Python
Django 返回json数据的实现示例
Mar 05 Python
Python类的继承super相关原理解析
Oct 22 Python
Django restful framework生成API文档过程详解
Nov 12 Python
Python可视化神器pyecharts之绘制箱形图
Jul 07 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
zend Framework中的Layout(模块化得布局)详解
2013/06/28 PHP
php+ajax实现的点击浏览量加1
2015/04/16 PHP
php实现和c#一致的DES加密解密实例
2017/07/24 PHP
Yii2框架实现登录、退出及自动登录功能的方法详解
2017/10/24 PHP
浅谈PHP中如何实现Hook机制
2017/11/14 PHP
PHP实现动态创建XML文档的方法
2018/03/30 PHP
php判断某个方法是否存在函数function_exists (),method_exists()与is_callable()区别与用法解析
2020/04/20 PHP
PHP实现随机发放扑克牌
2020/04/21 PHP
JS获取IUSR_机器名和IWAM_机器名帐号的密码
2006/12/06 Javascript
30个精美的jQuery幻灯片效果插件和教程
2011/08/23 Javascript
js 输出内容到新窗口具体实现代码
2013/05/31 Javascript
jquery.validate使用时遇到的问题
2015/05/25 Javascript
JavaScript、tab切换完整版(自动切换、鼠标移入停止、移开运行)
2016/01/05 Javascript
js从外部获取图片的实现方法
2016/08/05 Javascript
JS实现太极旋转思路分析
2016/12/09 Javascript
Angular的MVC和作用域
2016/12/26 Javascript
JavaScript错误处理和堆栈追踪详解
2017/04/18 Javascript
使用jQuery实现两个div中按钮互换位置的实例代码
2017/09/21 jQuery
详解mpvue开发小程序小总结
2018/07/25 Javascript
vue实现div拖拽互换位置
2020/07/29 Javascript
解决layui批量传值到后台操作时出现传值为空的问题
2019/09/28 Javascript
vue-devtools的安装和使用步骤详解
2019/10/17 Javascript
JS实现动态星空背景效果
2019/11/01 Javascript
Vue data的数据响应式到底是如何实现的
2020/02/11 Javascript
uniapp与webview之间的相互传值的实现
2020/06/29 Javascript
jQuery 添加元素和删除元素的方法
2020/07/15 jQuery
如何在现代JavaScript中编写异步任务
2021/01/31 Javascript
[04:56]经典回顾:前Ehome 与 前LGD
2015/02/26 DOTA
[01:00:22]DOTA2-DPC中国联赛定级赛 LBZS vs Magma BO3第三场 1月10日
2021/03/11 DOTA
Python中的闭包实例详解
2014/08/29 Python
利用python发送和接收邮件
2016/09/27 Python
python操作excel的包(openpyxl、xlsxwriter)
2018/06/11 Python
对Python w和w+权限的区别详解
2019/01/23 Python
python自动循环定时开关机(非重启)测试
2019/08/26 Python
Python类及获取对象属性方法解析
2020/06/15 Python
物流司机岗位职责
2013/12/28 职场文书