第十二章 图形用户界面
第十二章 圖形用戶界面
GUI就是包含按鈕、文本框等控件的窗口
Tkinter是事實(shí)上的Python標(biāo)準(zhǔn)GUI工具包
創(chuàng)建GUI示例應(yīng)用程序
初探
導(dǎo)入tkinter
import tkinter as tk也可導(dǎo)入這個(gè)模塊的所有內(nèi)容
from tkinter import *要?jiǎng)?chuàng)建GUI,可創(chuàng)建一個(gè)將充當(dāng)主窗口的頂級(jí)組件(控件)。
為此,可實(shí)例化一個(gè)Tk對(duì)象。
調(diào)用函數(shù)mainloop以進(jìn)入Tkinter主事件循環(huán),而不是直接退出程序。
from tkinter import * top = Tk() mainloop()效果圖如下:
創(chuàng)建按鈕,可實(shí)例化Button類。
需要使用布局管理器(也叫幾何體管理器)來(lái)顯示按鈕的位置(使用管理器pack)
按鈕也可以指定一些文本、給按鈕添加行為
效果圖如下:
可以不分別給屬性賦值,而使用方法config同時(shí)設(shè)置多個(gè)屬性。
button.config(text='Click me!',command=clicked)
還可使用控件的構(gòu)造函數(shù)來(lái)配置控件。
Button(text='click me too!',command=clicked).pack()
布局
對(duì)控件調(diào)用方法pack時(shí),將把控件放在其父控件(主控件)中。
from tkinter import * Label(text="I'm in the first window!").pack() second = Toplevel() Label(second,text="I'm in the second window!").pack()效果圖如下:
Toplevel類表示除主窗口外的另一個(gè)頂級(jí)窗口,而Label就是文本標(biāo)簽。
一列按鈕
from tkinter import * for i in range(10):Button(text=i).pack()mainloop()效果圖如下:
要快速了解可用的選項(xiàng),可執(zhí)行如下命令
還有其他的布局管理器,具體地說(shuō)是grid和place
help(Grid.configure) ''' Help on function grid_configure in module tkinter:grid_configure(self, cnf={}, **kw)Position a widget in the parent widget in a grid. Use as options:column=number - use cell identified with given column (starting with 0)columnspan=number - this widget will span several columnsin=master - use master to contain this widgetin_=master - see 'in' option descriptionipadx=amount - add internal padding in x directionipady=amount - add internal padding in y directionpadx=amount - add padding in x directionpady=amount - add padding in y directionrow=number - use cell identified with given row (starting with 0)rowspan=number - this widget will span several rowssticky=NSEW - if cell is larger on which sides will thiswidget stick to the cell boundary ''' help(Place.config) ''' Help on function place_configure in module tkinter:place_configure(self, cnf={}, **kw)Place a widget in the parent widget. Use as options:in=master - master relative to which the widget is placedin_=master - see 'in' option descriptionx=amount - locate anchor of this widget at position x of mastery=amount - locate anchor of this widget at position y of masterrelx=amount - locate anchor of this widget between 0.0 and 1.0relative to width of master (1.0 is right edge)rely=amount - locate anchor of this widget between 0.0 and 1.0relative to height of master (1.0 is bottom edge)anchor=NSEW (or subset) - position anchor according to given directionwidth=amount - width of this widget in pixelheight=amount - height of this widget in pixelrelwidth=amount - width of this widget between 0.0 and 1.0relative to width of master (1.0 is the same widthas the master)relheight=amount - height of this widget between 0.0 and 1.0relative to height of master (1.0 is the sameheight as the master)bordermode="inside" or "outside" - whether to take border width ofmaster widget into account '''事件處理
通過(guò)設(shè)置屬性command給按鈕指定動(dòng)作(action),這是一種特殊的事件處理。
Tkinter還提供了更通用的事件處理機(jī)制:方法bind。要讓控件對(duì)特定的事件進(jìn)行處理,可對(duì)其調(diào)用方法bind,并指定事件的名稱和要使用的函數(shù)。
點(diǎn)哪兒顯示所點(diǎn)擊的坐標(biāo)位置(鼠標(biāo)單擊事件,提供x和y坐標(biāo))
其中是使用鼠標(biāo)左按鈕(按鈕1)單擊的事件名稱。
將這種事件關(guān)聯(lián)到函數(shù)callback。
這樣,每當(dāng)用戶在窗口top中單擊時(shí),都將調(diào)用這個(gè)函數(shù)。向函數(shù)callback傳遞一個(gè)event對(duì)象,這個(gè)對(duì)象包含的屬性隨事件類型而異。
效果圖如下:
當(dāng)然也可以查詢幫助
最終的程序
簡(jiǎn)單的GUI文本編輯器
1,需要輸入所要編輯的文本地址
2,點(diǎn)擊Open,即可打開該文本文件
3,在下方編輯欄中可隨意編輯
4,點(diǎn)擊Save即可保存
效果圖如下:
小結(jié)
| 圖形用戶界面(GUI) | GUI有助于讓應(yīng)用程序?qū)τ脩舾押谩2⒎撬械某绦蚨夹枰狦UI,但只要程序需要與用戶交互,GUI就可能很有幫助。 |
| Tkinter | Tkinter是一個(gè)跨平臺(tái)的Python GUI工具包,成熟而且使用廣泛。 |
| 布局 | 通過(guò)指定組件的幾何屬性,很容易對(duì)其進(jìn)行定位,但要確保它們?cè)诟复翱诘拇笮“l(fā)生變化時(shí)做出正確的反應(yīng),就必須使用布局管理器。 |
| 事件處理 | GUI工具包中用戶觸發(fā)事件執(zhí)行的操作。要發(fā)揮作用,程序可能需要響應(yīng)某些事件,否則用戶將無(wú)法與之交互。在Tkinter中,要給組件添加事件處理程序,可使用方法bind。 |
總結(jié)
以上是生活随笔為你收集整理的第十二章 图形用户界面的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 第十一章 文件
- 下一篇: 第六章至第九章的单元测试