Python中使用Tkinter模块创建GUI程序实例
作者:junjie 发布时间:2023-01-22 09:37:38
标签:Python,Tkinter,GUI
使用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()
0
投稿
猜你喜欢
- 一、VScode下载官网Download Visual Studio Code - Mac, Linux, Windows点击64 bit会
- 1、配置环境支持python2和python3On Linux, Solaris, or FreeBSD, add the /usr/loc
- 前言问题:我们在放大图片的过程中,放大的目标图像和原图图像之间会存在不同。放大的基本思想:第一步:将目标图像进行缩小到原图像的尺寸,虚拟的将
- 本文实例讲述了Python实现批量下载图片的方法。分享给大家供大家参考。具体实现方法如下:#!/usr/bin/env python#-*-
- Pycharm是当前进行python开发,尤其是Django开发最好的IDE。GitHub是程序员的圣地,几乎人人都在用。本文假设你对pyc
- 1、选取最适用的字段属性MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就会越快。因此,在创建表
- 本文实例讲述了PHP面向对象程序设计子类扩展父类(子类重新载入父类)操作。分享给大家供大家参考,具体如下:在PHP中,会遇到这样的情况,子类
- Fuko Masked 是 Kaloyan Tsvetkov 的一个小型PHP库,用于通过用编辑后的元素替换列入黑名单的元素来屏蔽敏感数据。
- 前言图片的本质就是大量像素在二维平面上的组合,每个像素点用数字化方式记录颜色。可以直观的想象,一张图片就是一个巨大的电子栅格,每个格子内有一
- 今天看新概念视频的时候播放器PotPlayer的播放列表总是不能正确排序,我看到这些视频的名字格式如下:Lesson 1-2 单词解读.mp
- 1.python中列表list的拷贝,会有什么需要注意的呢? python变量名相当于标签名。list2=list1 ,直接赋值,实质上指向
- 一、PDF转为MP3 我们平常看到很多文件都是PDF格式,网上的各类书籍多为此格式。有时候不方便阅读,或者怕费眼睛伤颈椎,那么有没
- 首先,"/"左倾斜是正斜杠,"\"右倾斜是反斜杠,可以记为:除号是正斜杠一般来说对于目录分隔符,Un
- (一)、前言为什么引入消息队列?1.程序解耦2.提升性能3.降低多业务逻辑复杂度(二)、python操作rabbit mqrabbitmq配
- 【代码示例】 [code=SQL] DELIMITER $$ DROP FU
- 脚本内容代码如下:from mitmproxy import http, ctxfrom multiprocessing import Lo
- 今天偶然看到“一个有将近两年的div + CSS 开发经验和历史,曾经是Web标准绝对拥趸的同志”在自己的blog上发表放弃div+css的
- 类和对象类和函数一样都是Python中的对象。当一个类定义完成之后,Python将创建一个“类对象”并将其赋值给一个同名变量。类是type类
- 有时会统计某个目录下有哪些文件,每个文件的sha256及文件大小等相关信息,这里用python3写了个脚本用来实现此功能,此脚本可跨平台,同
- Dataframe结构放在numpy来看应该是二维矩阵的形式,每一列是一个特征,上面会有个列标题,每一行是一个样本。对Dataframe结构