1 / 14

Python GUI 工具包 :Tkinter

Python GUI 工具包 :Tkinter. Python 的标准图形工具包是 Tkinter, 它基于一种比较老的跨平台工具包 Tk 的 . 顾名思义 ,Tkinter 是 Python 程序中使用 Tk 的接口 . 为使用 Tkinter, 只需导入该模块 : import Tkinter 或者更常用的形式 : from Tkinter import *. 一个简单例子. from Tkinter import * root = Tk() w = Label(root, text="Hello, world!") w.pack()

brooklyn
Télécharger la présentation

Python GUI 工具包 :Tkinter

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python GUI工具包:Tkinter

  2. Python的标准图形工具包是Tkinter,它基于一种比较老的跨平台工具包Tk的. 顾名思义,Tkinter是Python程序中使用Tk的接口. • 为使用Tkinter,只需导入该模块: import Tkinter 或者更常用的形式: from Tkinter import * Lu Chaojun, SJTU

  3. 一个简单例子 from Tkinter import * root = Tk() w = Label(root, text="Hello, world!") w.pack() root.mainloop() 第二行:创建Tk根构件,一个普通窗口. 第三行:创建一个标签构件,它是根窗口的子构件. 第四行:调用标签构件的pack方法.这是布局管理器之一. 第五行:进入事件循环. Lu Chaojun, SJTU

  4. 又一个例子 from Tkinter import * class App: def __init__(self, master): frame = Frame(master) frame.pack() self.button1 = Button(frame, text="QUIT", fg="red", command=frame.quit) self.button1.pack(side=LEFT) self.button2 = Button(frame, text="Hello", command=self.say_hi) self.button2.pack(side=LEFT) def say_hi(self): print "hi there, everyone!“ root = Tk() app = App(root) root.mainloop() Lu Chaojun, SJTU

  5. 常用GUI构件(1) • 窗口:构件容器的一种. root = Tk() • 标签:文本或图像 Label(root, text = 'Hello Tkinter') Label(root, bitmap = 'error') • 按钮: def helloButton(): print 'hello button' Button(root, text = 'Hello', command = helloButton) Lu Chaojun, SJTU

  6. 常用GUI构件(2) • 文本输入: Entry(root) • 画布: cv = Canvas(root,bg='white') cv.pack() cv.create_rectangle(10,10,100,100) Lu Chaojun, SJTU

  7. 常用GUI构件(3) • 框架(frame):构件容器之一.多用于窗口布局. f = Frame(height=..., width=..., bg=...) Label(f, text=‘hello from frame’) • 顶层(top-level)窗口:类似Frame,但有窗口特征. t = Toplevel() Label(t, text=‘hello from Toplevel’) Lu Chaojun, SJTU

  8. 布局管理器 • 布局管理器Packer:默认上向下添加构件. <widget>.pack() print root.pack_slaves() • 布局管理器Place Label(root,text = 'hello Place').place(x=0, y=0, anchor = NW) • 布局管理器grid Label(root,text = 'Hello').grid(row=...,column=...) Lu Chaojun, SJTU

  9. 事件驱动编程

  10. 事件绑定函数 • GUI应用程序一般都有一个事件循环(通过mainloop方法进入). • 事件来源有多种,包括用户的按键和鼠标操作等. • Tkinter提供了强大机制使用户能自己处理事件:对每种构件,可将Python函数或方法绑定到事件上. Lu Chaojun, SJTU

  11. 事件 • 鼠标事件: • <Button-1>, <B1-Motion>, <Enter>, <Leave>,... • 例:event1.py • 键盘事件: • <Return>, <F5>, <‘a’>,<‘8’>,... • 总是发送到当前拥有键盘焦点的构件.可用<widget>.focus_set()将焦点移到某个构件 • 例:event2.py Lu Chaojun, SJTU

  12. 键盘事件处理 • 例:event3.py • 查看键盘输入事件并处理之,直至接收到退出事件(即空格键). • 事件处理:显示该键的ASCII码. • Python有一个函数getch()来每次读一个键. • Windows平台,此函数在msvcrt模块中. Lu Chaojun, SJTU

  13. GUI程序的事件处理 • 例:event4.py, event5.py • 创建应用类KeysApp,它在__init__方法中创建GUI,并且将空格键绑定到doQuitEvent方法. • 创建一个应用程序类是OO事件驱动环境中惯常的做法,因为事件送往程序和消息送往对象很相似 Lu Chaojun, SJTU

  14. End 14 Lu Chaojun, SJTU Lu Chaojun, SJTU

More Related