我对 python 和 tkinter 很陌生,我遇到了一个我无法弄清楚的问题。我有一个带有关联命令的按钮,它调用我的对象的方法。如果我按下按钮,我会得到我期望的结果。如果我再次按下按钮,我会收到一条错误消息:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
File "LED_GUI_1.py", line 115, in <lambda>
ttk.Button(mainframe, text="Red", command= lambda: Red.set_intensity(float(red_input.get()))).grid(column=1, row=3, sticky=(W, E))
AttributeError: 'float' object has no attribute 'set_intensity'
我彻底糊涂了。任何帮助表示赞赏。这是代码:
# -*- coding: utf-8 -*-
# RGB LED test GUI
from Tkinter import *
import ttk
Red = 0
Green = 0
Blue = 0
def test_pwm_set(channel, intensity):
global Red, Green, Blue
print "Calling test_pwm_set"
if channel == 0:
Red = intensity
print "Red = " + str(intensity)
elif channel == 1:
Green = intensity
print "Green = " + str(intensity)
elif channel == 2:
Blue = intensity
print "Blue = " + str(intensity)
else:
print 'Invalid Channel!'
def set_red():
intensity = float(red_input.get())
print intensity
print Red.get_intensity()
Red.set_intensity(50.0)
def set_green():
intensity = float(green_input.get())
Green.set_intensity(intensity)
def set_blue():
intensity = float(blue_input.get())
Blue.set_intensity(intensity)
class RGB_LED:
def __init__(self, color, channel, intensity = 0.0):
self.color = 'color'
self.channel = channel
self.intensity = intensity
def get_channel(self):
return self.channel
def get_intensity(self):
return self.intensity
def get_color(self):
return self.color
def set_channel(self, channel):
self.channel = channel
def set_intensity(self, intensity):
self.intensity = intensity
test_pwm_set(self.channel, self.intensity)
def set_color(self, color):
self.color = 'color'
Red = RGB_LED('Red', 0, 0.0)
Green = RGB_LED('Green', 1, 0.0)
Blue = RGB_LED('Blue', 2, 0.0)
######## set up GUI ##########
root = Tk()
root.title("RGB LED Control")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S)) #frame fills all of root window
mainframe.columnconfigure(0, weight=1) #tells Tk if root window is resized
mainframe.rowconfigure(0, weight=1) #the frame should take up the extra space
# StringVars
red_input = StringVar()
red_input.set(0.0)
green_input = StringVar()
green_input.set(0.0)
blue_input = StringVar()
blue_input.set(0.0)
red_intensity = StringVar()
green_intensity = StringVar()
blue_intensity = StringVar()
# Entry fields
red_entry = ttk.Entry(mainframe, width=5, textvariable=red_input)
red_entry.grid(column=1, row=2, sticky=(W, E))
green_entry = ttk.Entry(mainframe, width=5, textvariable=green_input)
green_entry.grid(column=2, row=2, sticky=(W, E))
blue_entry = ttk.Entry(mainframe, width=5, textvariable=blue_input)
blue_entry.grid(column=3, row=2, stick=(W, E))
# Labels
ttk.Label(mainframe, textvariable=red_intensity).grid(column=1, row=1, sticky=(W, E))
ttk.Label(mainframe, textvariable=green_intensity).grid(column=2, row=1, sticky=(W, E))
ttk.Label(mainframe, textvariable=blue_intensity).grid(column=3, row=1, sticky=(W, E))
# Buttons
ttk.Button(mainframe, text="Red", command= lambda: Red.set_intensity(float(red_input.get()))).grid(column=1, row=3, sticky=(W, E))
ttk.Button(mainframe, text="Green", command= lambda: Green.set_intensity(float(green_input.get()))).grid(column=2, row=3, sticky=(W, E))
ttk.Button(mainframe, text="Blue", command= lambda: Blue.set_intensity(float(blue_input.get()))).grid(column=3, row=3, sticky=(W, E))
root.mainloop()