TKinter Scale
TKinter Scale
觉得有用的话,欢迎一起讨论相互学习~Follow Me
感谢莫烦老师
详情
创建windows
window = tk.Tk()
window.title('my window')
window.geometry('500x500')
创建显示label
var1 = tk.StringVar()
l = tk.Label(window, bg='yellow', width=4, textvariable=var1)
l.pack()
循环刷新窗口
window.mainloop() # 不断刷新主窗口
显示函数
def print_selection(v):
l.config(text='you have selected ' + v)
显示Scale控件
# 注意:
# length表示的不是字符的宽度而是像素的宽度
# showvalue 指横轴上方是否显示当前数字,选择0是不显示,选择1是显示
# tickinterval 标签的单位长度 5-7-9-11
# resolution 表示精度,0.01表示保留两位小数
# command 表示调用的函数,默认传入值为scale标签标注的值
s = tk.Scale(window, label='try me', from_=5, to=11, orient=tk.HORIZONTAL,
length=200, showvalue=0, tickinterval=2, resolution=0.01, command=print_selection)
s.pack()
完整代码
# View more python learning tutorial on my Youtube and Youku channel!!!
# Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
# Youku video tutorial: http://i.youku.com/pythontutorial
import tkinter as tk
window = tk.Tk()
window.title('my window')
window.geometry('500x500')
l = tk.Label(window, bg='yellow', width=20, text='empty')
l.pack()
def print_selection(v):
l.config(text='you have selected ' + v)
# 注意:
# length表示的不是字符的宽度而是像素的宽度
# showvalue 指横轴上方是否显示当前数字,选择0是不显示,选择1是显示
# tickinterval 标签的单位长度 5-7-9-11
# resolution 表示精度,0.01表示保留两位小数
# command 表示调用的函数,默认传入值为scale标签标注的值
s = tk.Scale(window, label='try me', from_=5, to=11, orient=tk.HORIZONTAL,
length=200, showvalue=0, tickinterval=2, resolution=0.01, command=print_selection)
s.pack()
window.mainloop()