对于这条巨蟒2.7 Tkinter的代码,如果我输入“苹果”并点击“搜索”按钮,我应该从未知(“?”)重新设置字符串变量的选择和相关单选按钮来那些描述苹果(“脆皮”)和(“温和”),但我无法访问列表与我的if语句坐标列表。访问变量列表协调与if语句
from Tkinter import*
class Fruit:
def __init__(self, parent):
# variables
self.texture_option = StringVar()
self.climate_option = StringVar()
# layout
self.myParent = parent
self.main_frame = Frame(parent, background="light blue")
self.main_frame.pack(expand=YES, fill=BOTH)
texture_options = ["Soft", "Crunchy","?"]
climate_options = ["Temperate", "Tropical","?"]
self.texture_option.set("?")
self.climate_option.set("?")
self.texture_options_frame = Frame(self.main_frame, borderwidth=3, background="light blue")
self.texture_options_frame.pack(side=TOP, expand=YES, anchor=W)
Label(self.texture_options_frame, text="Texture:", relief=FLAT, font="bold", background="light blue").pack(side=LEFT,anchor=W)
for option in texture_options:
button = Radiobutton(self.texture_options_frame, text=str(option), indicatoron=0,
value=option, padx=5, variable=self.texture_option, background="light blue")
button.pack(side=LEFT)
self.climate_options_frame = Frame(self.main_frame, borderwidth=3, background="light blue")
self.climate_options_frame.pack(side=TOP, expand=YES, anchor=W)
Label(self.climate_options_frame, text="Climate:", relief=FLAT, font="bold", background="light blue").pack(side=LEFT,anchor=W)
for option in climate_options:
button = Radiobutton(self.climate_options_frame, text=str(option), indicatoron=0,
value=option, padx=5, variable=self.climate_option, background="light blue")
button.pack(side=LEFT)
#search button
self.search_frame = Frame(self.main_frame, borderwidth=5, height=50, background="light blue")
self.search_frame.pack(expand=NO)
self.enter = Entry(self.search_frame, width=30)
self.enter.pack(side=LEFT, expand=NO, padx=5, pady=5, ipadx=5, ipady=5)
self.searchbutton = Button(self.search_frame, text="Search", foreground="white", background="blue",
width=6, padx="2m", pady="1m")
self.searchbutton.pack(side=LEFT, pady=5)
self.searchbutton.bind("<Button-1>", self.searchbuttonclick)
self.searchbutton.bind("<Return>", self.searchbuttonclick)
def searchbuttonclick(self,event):
#fruit texture climate
fruit_bowl=[
('Apple', 'Crunchy','Temperate'),
('Orange', 'Soft','Tropical'),
('Pawpaw','Soft','Temperate')]
if self.enter.get()==fruit_bowl[i][0]:
self.texture_option.set(fruit_bowl[i][1])
self.climate_option.set(fruit_bowl[i][2])
root = Tk()
root.title("Fruit Bowl")
fruit = Fruit(root)
root.mainloop()
我想说,如果输入窗口等于0列对于任何给定行中fruit_bowl然后纹理选项设置到该行的第1列值和气候选项设置为第2列的值该行,但我怎么说在python中?
我最初省略了这段代码的gui组件来简化事情,但显然只是让所有事情变得更加复杂,并且使我的代码看起来不稳定和奇怪。上面的代码应该给你一个很好的GUI窗口,但击中搜索按钮,什么也不做,但产生以下错误消息:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\Lib\lib-tk\Tkinter.py", line 1403, in __call__
return self.func(*args)
File "F:\Python\fruit.py", line 59, in searchbuttonclick
if self.enter.get()==fruit_bowl[i][0]:
NameError: global name 'i' is not defined
是否有一个列表理解或东西,我可以用它来解决这个,而不是重写我的代码?这是一个模拟的例子,试图解决我用一个更大的模块所遇到的问题。
它看起来很奇怪,因为它是将字符串变量链接到单选按钮的较大代码的一部分。将发布整个事情。 – Jeff 2012-01-17 17:21:59
谢谢。如果现在不够好,我会尽量满足功能。随着我的技能提高,毫无疑问,代码将不再那么快乐。 – Jeff 2012-01-17 22:30:46