Python的GUI編程(二)Butto
- 2020 年 1 月 3 日
- 筆記
Button 控制項是一種標準 Tkinter 控制項, 用來展現不同樣式的按鈕. Button 控制項被用以和用戶交互, 比如按鈕被滑鼠點擊後, 某種操作被啟動. 和 Label 控制項類似, 按鈕可以展示圖片或者文字. 不同的是, Label 控制項可以指定字體, Button 控制項只能使用單一的字體. Button 上的文字可以多行顯示. 可以將一個 Python 函數或方法綁定到一個 Button 控制項. 這個函數或方法將在按鈕被點擊時執行.
按鈕Button控制項的屬性:
activebackground, activeforeground 類型:顏色; 說明:當按鈕被激活時所使用的顏色。 anchor 類型:常量; 說明:控制按鈕上內容的位置。使用N, NE, E, SE, S, SW, W, NW,or CENTER這些值之一。默認值是CENTER。 background (bg), foreground (fg) 類型:顏色; 說明:按鈕的顏色。默認值與特定平台相關。 bitmap 類型:點陣圖;
borderwidth (bd) 類型:整數; 說明:按鈕邊框的寬度。默認值與特定平台相關。但通常是1或2象素。 command 類型:回調; 說明:當按鈕被按下時所調用的一個函數或方法。所回調的可以是一個函數、方法或別的可調用的Python對象。 cursor 類型:游標; 說明:當滑鼠移動到按鈕上時所顯示的游標。 default 類型:常量; 說明:如果設置了,則按鈕為默認按鈕。注意這個語法在Tk 8.0b2中已改變。 disabledforeground 類型:顏色; 說明:當按鈕無效時的顏色。 font 類型:字體; 說明:按鈕所使用的字體。按鈕只能包含一種字體的文本。 highlightbackground, highlightcolor 類型:顏色; 說明:控制焦點所在的高亮邊框的顏色。當窗口部件獲得焦點的時候,邊框為highlightcolor所指定的顏色。否則邊框為highlightbackground所指定的顏色。默認值由系統所定。 highlightthickness 類型:距離; 說明:控制焦點所在的高亮邊框的寬度。默認值通常是1或2象素。 image 類型:圖象; 說明:在部件中顯示的圖象。如果指定,則text和bitmap選項將被忽略。 justify 類型:常量; 說明:定義多行文本如何對齊。可取值有:LEFT, RIGHT, 或 CENTER(默認)。 padx, pady 類型:距離; 說明:指定文本或圖象與按鈕邊框的間距。 relief 類型:常量; 說明:邊框的裝飾。通常按鈕按下時是凹陷的,否則凸起。另外的可能取值有GROOVE, RIDGE, 和 FLAT。 state 類型:常量; 說明:按鈕的狀態:NORMAL, ACTIVE 或 DISABLED。默認值為NORMAL。 takefocus 類型:標誌; 說明:表明用戶可以Tab鍵來將焦點移到這個按鈕上。默認值是一個空字元串,意思是如果按鈕有按鍵綁定的話,它可以通過所綁定的按鍵來獲得焦點。 text 類型:字元串; 說明:顯示在按鈕中的文本。文本可以是多行。如果bitmaps或image選項被使用,則text選項被忽略。 textvariable 類型:變數; 說明:與按鈕相關的Tk變數(通常是一個字元串變數)。如果這個變數的值改變,那麼按鈕上的文本相應更新。 underline 類型:整數; 說明:在文本標籤中哪個字元加下劃線。默認值為-1,意思是沒有字元加下劃線。 width, height 類型:距離; 說明:按鈕的尺寸。如果按鈕顯示文本,尺寸使用文本的單位。如果按鈕顯示圖象,尺寸以象素為單位(或螢幕的單位)。如果尺寸沒指定,它將根據按鈕的內容來計算。 wraplength 類型:距離; 說明:確定一個按鈕的文本何時調整為多行。它以螢幕的單位為單位。默認不調整。
點擊Button,利用回調函數顯示文本內容。
from Tkinter import * Bu=Tk() #回調函數 def PrintButton(): print '荷塘花!' img=PhotoImage(file='D:/temp/1.gif') Button(Bu,width=200,height=200,text='press',anchor='c',bg='blue',fg='red',padx=120,pady=120,borderwidth=10,relief='ridge',image=img,compound='bottom',command=PrintButton).pack() Bu.mainloop()
anchor屬性:
from Tkinter import * root = Tk() for a in ['n','s','e','w','ne','nw','se','sw']: Button(root, text = 'anchor', anchor = a, width = 30, height = 4).pack() #文本顯示的位置。 Button(root,text = 'anchor',width = 30,height =4).pack() Button(root,text = 'anchor',anchor = 'center',width = 30,height =4).pack() Button(root,text = 'anchor',anchor = 'n',width = 30,height = 4).pack() Button(root,text = 'anchor',anchor = 's',width = 30,height = 4).pack() Button(root,text = 'anchor',anchor = 'e',width = 30,height = 4).pack() Button(root,text = 'anchor',anchor = 'w',width = 30,height = 4).pack() Button(root,text = 'anchor',anchor = 'ne',width = 30,height = 4).pack() Button(root,text = 'anchor',anchor = 'nw',width = 30,height = 4).pack() Button(root,text = 'anchor',anchor = 'se',width = 30,height = 4).pack() Button(root,text = 'anchor',anchor = 'sw',width = 30,height = 4).pack() root.mainloop()
利用按鈕退出Label計時器:
def after(self, ms, func=None, *args): """Call function once after given time. MS specifies the time in milliseconds. FUNC gives the function which shall be called. Additional parameters are given as parameters to the function call. Return identifier to cancel scheduling with after_cancel."""
after(self, ms, func=None, *args) Tkinter的方法。標籤實例 在給定時間後調用函數。MS以毫秒為單位指定時間。函數給出了響應調用的函數。額外的參數作為函數調用的參數。返回使用after_cancel取消調度的標識符。 if not func: # I'd rather use time.sleep(ms*0.001) self.tk.call('after', ms) else: def callit(): try: func(*args) finally: try: self.deletecommand(name) except TclError: pass callit.__name__ = func.__name__ name = self._register(callit) return self.tk.call('after', ms, name)
回調函數與函數:fun與fun()作為參數時表示的意義不同。
fun作為參數表示是函數
fun()作為參數時表示一個值
config(self, cnf=None, **kw) Tkinter方法。標籤實例 配置小部件的資源。資源的值被指定為關鍵字。 就是使用config來重新給標籤屬性賦值
程式暫停的幾種方法:
1、導入os模組
import os os.system('pause)
2、導入subprocess模組
import subprocess subprocess.call("pause",shell=True)
3、input();
這種方法不用包含模組,因此這也是最常用的一種暫停手段。
Python2中的raw_input()和input()語句在Python3中已經被合併到input()中。
程式退出方法: 1、導入os模組
import os
os._exit()
os._exit()會直接將python程式終止,之後的所有程式碼都不會繼續執行。
2、導入sys模組
import sys
sys.exit()
sys.exit()會引發一個異常:SystemExit,如果這個異常沒有被捕獲,那麼python解釋器將會退出。如果有捕獲此異常的程式碼,那麼這些程式碼還是會執行。
計時器示例:
from Tkinter import * import subprocess import os import sys counter = 0 def counter_label(label): counter = 0 def count(): global counter counter += 1 #配置屬性 #區間大小 label.config(width=10, height=2) #文本內容 label.config(text=str(counter/3600%24/10)+str(counter/3600%24%10)+':'+str(counter/60%60/10)+str(counter/60%60%10)+':'+str(counter%60/10)+str(counter%60%10)) #字體顏色 label.config(fg='red') #label位置 label.config(anchor='c') #after函數的第一個參數設置毫秒數後,調用count函數 label.after(1, count) count() def Pause(): # subprocess.call("pause",shell=True) # os.system('pause') # input() sys.exit() root = Tk() root.title("計時器") label = Label(root) label.pack(side='left') #查找方法或屬性 # print help(label.config) # print help(label.after) counter_label(label) button = Button(root, text='Stop', width=5, command=Pause,anchor='c').pack(side='right')#或command=root.destory小伙窗口 root.mainloop()

參考:http://blog.csdn.net/liuxu0703/article/details/60639166