문제

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