共計 624 個字符,預計需要花費 2 分鐘才能閱讀完成。
Tkinter 是 Python 的標準 GUI(圖形用戶界面)包,用于創建和管理窗口應用程序。它提供了一組用于創建 GUI 應用程序的類和方法。
以下是 Tkinter 模塊的一些常用用法:
-
導入 Tkinter 模塊:
from tkinter import *
-
創建主窗口:
root = Tk()
-
添加組件到窗口:
label = Label(root, text="Hello, tkinter!") label.pack()
-
設置窗口的標題:
root.title("My Application")
-
運行窗口的消息循環:
root.mainloop()
-
創建按鈕并添加事件處理函數:
def button_click(): print("Button clicked!") button = Button(root, text="Click Me", command=button_click) button.pack()
-
創建文本框并獲取用戶輸入:
entry = Entry(root) entry.pack() def get_input(): user_input = entry.get() print("User input:", user_input) button = Button(root, text="Get Input", command=get_input) button.pack()
這只是 Tkinter 模塊的一小部分功能,它還提供了其他的組件、布局管理器、事件處理等功能,可以根據具體需求進行使用。
丸趣 TV 網 – 提供最優質的資源集合!
正文完