如何將我的 Tkinter Scale 小部件滑塊的預設值設定為 100?


Tkinter Scale 小部件用於提供資料項的可視表示,我們可以在其中修改或更改資料項的值。為了更改資料的狀態或選擇欄位中的多個數據,我們可以使用 **Scale** 小部件。

可以透過初始化 **Scale(parent, from_, to, orient, options)** 建構函式建立 Scale 小部件。

但是,有時我們需要設定 Tkinter Scale 小部件的預設值,可以透過使用 **set(numeric_value)** 方法來設定。

示例

在此示例中,我們將建立一個為滑塊設定預設值的比例小部件。

#Import the required Libraries
from tkinter import *
from tkinter import ttk
#Import the required Libraries
from tkinter import *
from tkinter import ttk
#Create an instance of tkinter frame
win = Tk()
#Set the geometry of tkinter frame
win.geometry("750x250")
#Define a function to reset the slider
def reset_slider():
   horizontal.set(100)
#Initialize a Horizontal Scale Widget
horizontal=Scale(win, from_=0, to=200, orient= HORIZONTAL)
#Set the Default Value of Scale Widget
horizontal.set(100)
horizontal.pack()
#Create a button to reset the slider
ttk.Button(win, text= "Reset", command= reset_slider).pack(pady=15)
win.mainloop()

輸出

現在,執行以上程式碼以顯示具有某些預設值的滑塊。

在給定的輸出中,當我們修改滑塊值時,我們可以透過單擊“重置”按鈕來重置它。

更新於: 2021 年 5 月 3 日

1K+ 檢視

開啟你的 職業生涯

完成課程可獲得認證

開始
廣告
© . All rights reserved.