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 相关文章推荐
在Django中编写模版节点及注册标签的方法
Jul 20 Python
Python中使用多进程来实现并行处理的方法小结
Aug 09 Python
python实现画五角星和螺旋线的示例
Jan 20 Python
Python实现图片转字符画的代码实例
Feb 22 Python
Django使用模板后无法找到静态资源文件问题解决
Jul 19 Python
pymysql 开启调试模式的实现
Sep 24 Python
Python3 sys.argv[ ]用法详解
Oct 24 Python
Python3 读取Word文件方式
Feb 13 Python
详细分析Python垃圾回收机制
Jul 01 Python
Python正则re模块使用步骤及原理解析
Aug 18 Python
python pip如何手动安装二进制包
Sep 30 Python
Pandas之缺失数据的实现
Jan 06 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
2.PHP入门
2006/10/09 PHP
PHP中exec与system用法区别分析
2014/09/22 PHP
PHP实现文件下载详解
2014/11/27 PHP
PHP Header用于页面跳转时的几个注意事项
2016/10/21 PHP
php mysql实现mysql_select_db选择数据库
2016/12/30 PHP
php中目录操作opendir()、readdir()及scandir()用法示例
2019/06/08 PHP
硬盘浏览程序,保存成网页格式便可使用
2006/12/03 Javascript
一个刚完成的layout(拖动流畅,不受iframe影响)
2007/08/17 Javascript
基于jquery的气泡提示效果
2010/05/31 Javascript
javascript (用setTimeout而非setInterval)
2011/12/28 Javascript
捕获键盘事件(且兼容各浏览器)
2013/07/03 Javascript
JavaScript中setUTCMilliseconds()方法的使用详解
2015/06/12 Javascript
jQuery实现页面顶部显示的进度条效果完整实例
2015/12/09 Javascript
深入浅析AngularJS和DataModel
2016/02/16 Javascript
Angular2内置指令NgFor和NgIf详解
2016/08/03 Javascript
AngularJS监听路由的变化示例代码
2016/09/23 Javascript
JavaScript面向对象分层思维全面解析
2016/11/22 Javascript
详解Vue中使用v-for语句抛出错误的解决方案
2017/05/04 Javascript
基于Vue 2.0的模块化前端 UI 组件库小结
2017/12/21 Javascript
vue 之 .sync 修饰符示例详解
2018/04/21 Javascript
浅谈关于iview表单验证的问题
2018/09/29 Javascript
原生js实现each方法实例代码详解
2019/05/27 Javascript
javascript实现自由编辑图片代码详解
2019/06/21 Javascript
vue.js购物车添加商品组件的方法
2019/09/17 Javascript
Python数据分析之双色球统计单个红和蓝球哪个比例高的方法
2018/02/03 Python
解决Pycharm出现的部分快捷键无效问题
2018/10/22 Python
Python修改文件往指定行插入内容的实例
2019/01/30 Python
浅谈Pandas Series 和 Numpy array中的相同点
2019/06/28 Python
python实现倒计时小工具
2019/07/29 Python
使用Python爬取弹出窗口信息的实例
2020/03/14 Python
tensorflow实现将ckpt转pb文件的方法
2020/04/22 Python
python PyAUtoGUI库实现自动化控制鼠标键盘
2020/09/09 Python
公务员个人自我评价分享
2013/11/06 职场文书
2016七一建党节慰问信
2015/11/30 职场文书
Nginx配置https原理及实现过程详解
2021/03/31 Servers
详解MySQL集群搭建
2021/05/26 MySQL