1. 程式人生 > >GUI篇 tkinter (Label,Button)之一

GUI篇 tkinter (Label,Button)之一

說明 lac shift 接受 orm inter rcu 裏的 alt

import tkinter
from tkinter import *

# tkinter._test()

# 實例化一個窗口對象
base = tkinter.Tk()
# 修改窗口的標題
base.wm_title("窗口")

# 組件的使用

"""
# Label 組件
# 組件的使用都是相似的,在實例化這些組件的時候,第一個參數都是父窗口或者父組件,後面跟著的就是該組件的一些屬性。
# 比如:Label 的 text 屬性和 background 屬性
w1 = Label(base,text = "跟著星哥一起學tkingter", background= "green")
w2 = Label(base,text = "我愛python,因為它簡潔", background= "red")
w3 = Label(base,text = "開創夢想,從現在做起", background= "yellow")

# pack方法布局。也可以使用 place 和 grid 來布局管理
w1.pack()
w2.pack()
w3.pack()
"""

# Button組件


‘‘‘
綁定方式通常有如下幾種:
第一種,在按鈕組件被聲明的時候用command屬性聲明,command屬性接受一個函數名,註意函數名不要加雙引號。
第二種,使用bind方法,該方法是Misc這個類的一個方法,
‘‘‘

# 第一種綁定事件方式
# 註意command屬性後面不要加任何的標點符號
‘‘‘
def xinLabel():
global base
s = Label(base,text="我愛python",background="green")
s.pack()

b1 = Button(base,text="按鈕",command=xinLabel)
b1.pack()
‘‘‘

# 第二種綁定事件方式
def xinLabel(event):
global base
s = Label(base,text="我愛python",background="green")
s.pack()

b1 = Button(base,text="按鈕")
# bind 的第一個參數是事件類型,它采用的描述方式是這 樣的:<MODIFIER-MODIFIER-TYPE-DETAIL>,這裏的
# MODIFIER 即修飾符,它的全部取值如下:Control, Mod2, M2, Shift, Mod3, M3, Lock, Mod4, M4, Button1, B1, Mod5, M5 Button2, B2, Meta, M, Button3,B3, Alt, Button4, B4, Double, Button5, B5 Triple , Mod1, M1 。 而第三個 TYPE 表示類型,它的全部取值如下:Activate, Enter, Map, ButtonPress, Button, Expose, Motion,
# ButtonRelease,FocusIn, MouseWheel, Circulate, FocusOut, Property, Colormap, Gravity Reparent, Configure, KeyPress, Key, Unmap, Deactivate, KeyRelease Visibility, Destroy,Leave。第三個參數表 示細節,其實也就是對第二個參數的一些輔助說明。
# 第一個參數可能對剛使用它的人來說有點太復雜了,常見的鼠標左鍵單擊如下:<Button-1>,
# 也就是我上面的代碼中用到的
# 第二個參數可以是一個函數名,記住,不要加任何的標點符號,否則運行時會報錯的。
# 使用 bind 函數的時候,第二個參數是一個函數名,該函數必須接受一個參數,即表示該事件
b1.bind("<Button-1>",xinLabel)
b1.pack()


# 消息循環
base.mainloop()

GUI篇 tkinter (Label,Button)之一