Python的GUI编程

简介: Python的GUI编程 使用Tkinter模块来创建简单的GUI程序。Tkinter的Widgets有:Button、Canvas、Checkbutton、Entry、Frame、Label、Listbox、Menu、Menubutton、Message、Radiobutton、Scales、Scrollbar、TEXT、Toplevel等。

Python的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()
目录
相关文章
|
15天前
|
安全 Java 数据处理
Python网络编程基础(Socket编程)多线程/多进程服务器编程
【4月更文挑战第11天】在网络编程中,随着客户端数量的增加,服务器的处理能力成为了一个重要的考量因素。为了处理多个客户端的并发请求,我们通常需要采用多线程或多进程的方式。在本章中,我们将探讨多线程/多进程服务器编程的概念,并通过一个多线程服务器的示例来演示其实现。
|
15天前
|
程序员 开发者 Python
Python网络编程基础(Socket编程) 错误处理和异常处理的最佳实践
【4月更文挑战第11天】在网络编程中,错误处理和异常管理不仅是为了程序的健壮性,也是为了提供清晰的用户反馈以及优雅的故障恢复。在前面的章节中,我们讨论了如何使用`try-except`语句来处理网络错误。现在,我们将深入探讨错误处理和异常处理的最佳实践。
|
19天前
|
缓存 监控 Python
解密Python中的装饰器:优雅而强大的编程利器
Python中的装饰器是一种强大而又优雅的编程工具,它能够在不改变原有代码结构的情况下,为函数或类添加新的功能和行为。本文将深入解析Python装饰器的原理、用法和实际应用,帮助读者更好地理解和利用这一技术,提升代码的可维护性和可扩展性。
|
1月前
|
编译器 测试技术 C++
【Python 基础教程 01 全面介绍】 Python编程基础全攻略:一文掌握Python语法精髓,从C/C++ 角度学习Python的差异
【Python 基础教程 01 全面介绍】 Python编程基础全攻略:一文掌握Python语法精髓,从C/C++ 角度学习Python的差异
167 0
|
23小时前
|
机器学习/深度学习 人工智能 数据可视化
Python:探索编程之美
Python:探索编程之美
9 0
|
1天前
|
机器学习/深度学习 人工智能 数据处理
Python编程的魅力与实践
Python编程的魅力与实践
|
2天前
|
SQL 关系型数据库 MySQL
第十三章 Python数据库编程
第十三章 Python数据库编程
|
2天前
|
存储 网络协议 关系型数据库
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
Python从入门到精通:2.3.2数据库操作与网络编程——学习socket编程,实现简单的TCP/UDP通信
|
7天前
|
安全 数据处理 开发者
《Python 简易速速上手小册》第7章:高级 Python 编程(2024 最新版)
《Python 简易速速上手小册》第7章:高级 Python 编程(2024 最新版)
19 1
|
7天前
|
人工智能 数据挖掘 程序员
《Python 简易速速上手小册》第1章:Python 编程入门(2024 最新版)
《Python 简易速速上手小册》第1章:Python 编程入门(2024 最新版)
35 0