為什麼我們在 Tkinter 中使用 import * 然後是 ttk?
為了使用 tkinter 應用程式,我們必須在我們的環境中安裝並匯入 tkinter 庫。通常,我們使用 **from tkinter import** * 命令在環境中匯入 tkinter 庫。
**"import *"** 的意義在於它代表了 tkinter 庫中的所有函式和內建模組。透過匯入所有函式和方法,我們可以在特定應用程式中使用內建函式或方法,而無需隱式匯入它們。
tkinter 庫中提供了許多小部件、函式和方法,可用於構建特定應用程式的元件。Tkinter 提供了 **ttk** 包,用於設定小部件的屬性及其外觀和風格。為了使用 **ttk** 包,我們必須透過鍵入以下程式碼來匯入它:
from tkinter import ttk
示例
在這個特定的示例中,我們將建立一個功能性應用程式,其中包含一個按鈕和小部件標籤。
#Import tkinter library
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter frame or window
win= Tk()
#Set the geometry of tkinter frame
win.geometry("750x250")
#Define the function to close the window
def change_text():
label.configure(text="Welcome")
#Create a label
label=Label(win, text= "Click the below button to Change this Text", font=('Aerial 20 bold'))
label.pack(pady=30)
#Create a button widget
button= ttk.Button(win, text="Commit",command=lambda:change_text())
button.pack()
win.mainloop()輸出
執行以上程式碼將顯示一個視窗,其中包含一個按鈕和一個顯示某些文字的文字標籤。當我們點選按鈕時,它將更改螢幕上的訊息。

現在,點選“提交”按鈕來更改標籤文字。

廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP