如何将我的 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.