[英]How to print the value of a tkinter Scale widget?
Can somebody tell me why this will not print the value? 有人可以告诉我为什么这将不打印值吗? I want to print the output of the
Scale
widget every time it changes. 我想在每次更改时打印
Scale
小部件的输出。
from tkinter import *
master= Tk()
master.geometry('500x500+0+0')
def print_value(val):
print val
c1 = Scale(master, from_=255, to=0, length =400,width =100, troughcolor = 'blue',command=print_value)
c1.grid(row=1,column=2)
root.mainloop()
If you're using Python 3
then you should change print val
to print(val)
as in Python3 print
is a function, not an operator. 如果您使用的是
Python 3
,则应将print val
更改为print(val)
就像在Python3中一样, print
是一个函数,而不是运算符。
Also you should probably replace root
with master
on the last line as there is no root
variable in your code. 另外,由于代码中没有
root
变量,因此您可能应该在最后一行将root
替换为master
。
Using Python 2.7 with this code works perfect: 结合使用Python 2.7和以下代码可以完美工作:
import Tkinter
master= Tkinter.Tk()
master.geometry('500x500+0+0')
def print_value(val):
print val
c1 = Tkinter.Scale(master, from_=255, to=0, length =400,width =100, troughcolor = 'blue',command=print_value)
c1.grid(row=1,column=2)
master.mainloop()
I don't have Python 3 here but it should work like this: 我这里没有Python 3,但它应该像这样工作:
from tkinter import *
master= Tk()
master.geometry('500x500+0+0')
def print_value(val):
print (val)
c1 = Scale(master, from_=255, to=0, length =400,width =100, troughcolor = 'blue',command=print_value)
c1.grid(row=1,column=2)
main.mainloop()
You have to change you last line: 您必须更改最后一行:
root.mainloop()
by 通过
master.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.