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.

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.

Also you should probably replace root with master on the last line as there is no root variable in your code.

其他提示

You have to change you last line:

root.mainloop()

by

master.mainloop()

Using Python 2.7 with this code works perfect:

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:

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()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top