1

有人能告诉我为什么这不会打印价值吗?我想在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()
4

3 回答 3

2

如果您正在使用,Python 3那么您应该更改print valprint(val)Python3print中的函数,而不是运算符。

此外,您可能应该在最后一行替换root为,因为您的代码master中没有root变量。

于 2013-12-24T16:04:35.233 回答
0

在此代码中使用 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()

我这里没有 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()
于 2013-12-24T16:00:59.043 回答
0

你必须改变你的最后一行:

root.mainloop()

经过

master.mainloop()
于 2013-12-24T16:04:39.793 回答