简体   繁体   中英

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.

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.

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()

You have to change you last line:

root.mainloop()

by

master.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM