For this python 2.7 tkinter code, if I enter ‘Apple’ and hit the ‘Search’ button, I should reset the string variable options and associated radiobuttons from unknown (“?”) to those describing apples (“Crunchy”) and (“Temperate”) but I’m having trouble accessing list of lists coordinates with my if statement.
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()
I want to say if the entry window equals column 0 for any given row in fruit_bowl then the texture option sets to the column 1 value for that row and the climate option sets to the column 2 value for that row, but how do I say that in python?
I had originally ommitted the gui components of this code to simplify things, but apparently just made everything more complicated and made my code look choppy and odd. The code above should give you a nice GUI window, but hitting the search button does nothing but generate the following error message:
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
Is there a list comprehension or something I could use to settle this instead of rewriting my code? This is a mock example to try and settle issues I’m having with a much larger module.
You example-code is not very consistent. For example you define an
StingVarobject, which would make sense if you would couple the object with an tkinter widget like theEntrywidget:considering that, i would ommit your head-part and do it like:
If you want to keep your code as it is you would have to make something like the following:
Where did you define the variable
i? I can’t see a definition and so can’t python either.To correct the situation i made an iteration over your
fruit_bowland assigned the valueof the actual tuple-index in the list to the variable `i.
That are the only two lines you would have to add (except the added identations of the following lines) to make your code work. It’s not ver elegant, but maybe you can learn something out of it.
alternativly you could also consider doint this:
If you have further question, just post a comment and i will update my answer accordingly.