Scale控件是设置一个滑动栏,我们可以设置起始坐标值,终止坐标值,分度,还需要掌握如何获取滑块的值。
示例代码如下:
from tkinter import *
class Application(Frame):
def __init__(self,master=None):
super().__init__(master)
self.master = master
self.pack()
self.createwidget()
def createwidget(self):
v = StringVar()
# 起始坐标点为10,终止坐标值为50,长度为200个像素,分度为5,水平显示(默认为垂直)
sc = Scale(self, from_=10, to=50, length=200,tickinterval=5,
orient=HORIZONTAL, variable=v, command=self.test)
sc.pack()
# print(v.get()) # 获取变量初值
self.l = Label(self,text="你瞅啥", width=10, height=1, bg="black", fg="white")
self.l.pack()
def test(self,value):
print("滑块的值为:", value) # 显示当前的滑块坐标值
newFont = ("宋体", value) # 重新设置字体格式为:宋体,大小为"value"
self.l.config(font=newFont) # 重新设置
if __name__ == '__main__':
root = Tk()
root.title("Scale测试")
root.geometry("300x200")
app = Application(root)
root.mainloop()
程序运行结果,初始时
拖动滑块到40显示如下:
拖动到50显示如下: