1

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()
finefoot
  • 9,914
  • 7
  • 59
  • 102
user2996828
  • 1,137
  • 3
  • 12
  • 19
  • 2
    What version are you using? `tkinter` isn't a valid module name in 2.7, and `print val` isn't valid syntax in 3.X. Also, where is `root` defined? Please provide runnable code that exhibits your problem. – Kevin Dec 24 '13 at 15:58
  • 2
    In future, please provide all pertinent information, including the precise error message. – Robᵩ Dec 24 '13 at 16:05

3 Answers3

2

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.

sasha.sochka
  • 14,395
  • 10
  • 44
  • 68
0

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()
Emi
  • 525
  • 1
  • 3
  • 21
0

You have to change you last line:

root.mainloop()

by

master.mainloop()
Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60