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 制作图片转pdf工具
Jan 30 Python
处理Python中的URLError异常的方法
Apr 30 Python
python实现计算倒数的方法
Jul 11 Python
Python制作钉钉加密/解密工具
Dec 07 Python
Python轻量级ORM框架Peewee访问sqlite数据库的方法详解
Jul 20 Python
Python运维自动化之nginx配置文件对比操作示例
Aug 29 Python
python中的数据结构比较
May 13 Python
对numpy下的轴交换transpose和swapaxes的示例解读
Jun 26 Python
Python将列表中的元素转化为数字并排序的示例
Dec 25 Python
python匿名函数lambda原理及实例解析
Feb 07 Python
Python如何实现后端自定义认证并实现多条件登陆
Jun 22 Python
python基于opencv实现人脸识别
Jan 04 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
解析Extjs与php数据交互(增删查改)
2013/06/25 PHP
PHP实现的redis主从数据库状态检测功能示例
2017/07/20 PHP
php下的原生ajax请求用法实例分析
2020/02/28 PHP
JavaScript DOM学习第六章 表单实例
2010/02/19 Javascript
写js时遇到的一些小问题
2010/12/06 Javascript
JQuery之focus函数使用介绍
2013/08/20 Javascript
jsonp原理及使用
2013/10/28 Javascript
写出高效jquery代码的19条指南
2014/03/19 Javascript
php读取sqlite数据库入门实例代码
2014/06/25 Javascript
JavaScript事件学习小结(三)js事件对象
2016/06/09 Javascript
JS判断时间段的实现代码
2017/06/14 Javascript
基于angular2 的 http服务封装的实例代码
2017/06/29 Javascript
Bootstrap Table 在指定列中添加下拉框控件并获取所选值
2017/07/31 Javascript
2种简单的js倒计时方式
2017/10/20 Javascript
Vue+SpringBoot开发V部落博客管理平台
2017/12/27 Javascript
puppeteer实现html截图的示例代码
2019/01/10 Javascript
详解一个基于套接字实现长连接的express
2019/03/28 Javascript
[03:07]DOTA2英雄基础教程 冰霜诅咒极寒幽魂
2013/12/06 DOTA
python 多进程通信模块的简单实现
2014/02/20 Python
Python使用matplotlib绘制正弦和余弦曲线的方法示例
2018/01/06 Python
Python比较2个时间大小的实现方法
2018/04/10 Python
python抽取指定url页面的title方法
2018/05/11 Python
python 多线程重启方法
2019/02/18 Python
Python中的 ansible 动态Inventory 脚本
2020/01/19 Python
python给图像加上mask,并提取mask区域实例
2020/01/19 Python
澳大利亚首个在线预订旅游网站:Wotif
2017/07/19 全球购物
Book Depository欧盟:一家领先的国际图书零售商
2019/05/21 全球购物
荷兰鞋类购物网站:Donelli
2019/05/24 全球购物
自我评价个人范文
2013/12/16 职场文书
交通安全标语
2014/06/06 职场文书
孝敬父母的活动方案
2014/08/28 职场文书
退学证明范本3篇
2014/10/29 职场文书
幼儿园个人总结
2015/02/28 职场文书
保护环境建议书作文400字
2015/09/14 职场文书
2016年党员公开承诺书格式范文
2016/03/24 职场文书
解决Vue+SpringBoot+Shiro跨域问题
2021/06/09 Vue.js