简体   繁体   English

如何打印tkinter Scale小部件的值?

[英]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.

相关问题 如何从 tkinter Scale 小部件显示中删除比例值? - How to remove scale value from tkinter Scale widget display? 如何根据Python中另一个tkinter`Scale`小部件的值来控制tkinter`Scale`小部件的值? - How control the value of a tkinter `Scale` widget depending on the value of another tkinter `Scale` widget in Python? 如何在Python中为tkinter Scale小部件设置初始值? - How set the initial value for a tkinter Scale widget in Python? 如何使用tkinter Scale小部件的值连续更改图像 - How to continuously change an image using the value of a tkinter Scale widget 如何在Python中检索tkinter ttk Scale小部件的整数值? - How to retrieve the integer value of tkinter ttk Scale widget in Python? Tkinter:如何使用 scale-widget 的值来更改 label-widget 的文本? - Tkinter: How do I use the value of a scale-widget to change the text of a label-widget? 如何在Tkinter中使用Scale小部件更新matplotlib - How to update matplotlib with Scale widget in Tkinter 如何将Tkinter Scale小部件的状态设置为Disabled? - How to set the state of a tkinter Scale widget to disabled? Tkinter 缩放小部件标签 - Tkinter scale widget labeling 如何更改显示tkinter Scale小部件当前值的一侧? - How to change the side where the current value of the tkinter Scale widget is being displayed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM