• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python中使用Tkinter模块创建GUI程序实例

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

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了python gui tkinter,python tkinter模块,python tkinter,python tkinter教程,python tkinter安装等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

使用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()
</div>
例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()
</div>
例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()
</div>
例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()
</div>
例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()
</div>
例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()
</div>
按钮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()
</div>
例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()
</div>
用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()
  
  Tkin

您可能想查找下面的文章:

  • Python Tkinter GUI编程入门介绍
  • Python中使用Tkinter模块创建GUI程序实例
  • Python中使用Tkinter模块创建GUI程序实例

相关文章

  • python根据时间生成mongodb的ObjectId的方法
  • 浅谈python中截取字符函数strip,lstrip,rstrip
  • Python多进程同步Lock、Semaphore、Event实例
  • 使用Python来编写HTTP服务器的超级指南
  • Python lxml模块安装教程
  • python Selenium爬取内容并存储至MySQL数据库的实现代码
  • Python的shutil模块中文件的复制操作函数详解
  • Python使用MySQLdb for Python操作数据库教程
  • 爬山算法简介和Python实现实例
  • Python简单进程锁代码实例

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 使用Python导出Excel图表以及导出为图片的方法
    • python抓取网页图片示例(python爬虫)
    • Pyramid将models.py文件的内容分布到多个文件的方法
    • Python Web框架Flask中使用七牛云存储实例
    • 使用Python编写提取日志中的中文的脚本的方法
    • Python StringIO模块实现在内存缓冲区中读写数据
    • python编写简单爬虫资料汇总
    • 详解Django中的权限和组以及消息
    • python通过ftplib登录到ftp服务器的方法
    • python下读取公私钥做加解密实例详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有