Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7076867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:21:03+00:00 2026-05-28T06:21:03+00:00

For this python 2.7 tkinter code, if I enter ‘Apple’ and hit the ‘Search’

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:21:03+00:00Added an answer on May 28, 2026 at 6:21 am

    You example-code is not very consistent. For example you define an StingVar object, which would make sense if you would couple the object with an tkinter widget like the Entry widget:

    self.entry_var = StringVar()
    self.enter = Entry(root, width = 30, textvariable = self.entry_var)
    selection = self.entry_var.get()
    

    considering that, i would ommit your head-part and do it like:

    self.enter = Entry(root, width=30)
    self.enter.pack(side=LEFT, expand=NO)
    
    #fruit  texture  climate 
    fruit_bowl={'apple': ('Crunchy','Temperate'),
                'orange': ('Soft','Tropical'),
                'pawpaw': ('Soft','Temperate')}
    
    selection = self.enter.get()
    try:
       self.texture_option = fruit_bowl[selection.lower()][0]
       self.climate_option = fruit_bowl[selection.lower()][1]
       self.fruit_option = selection.capitalize()
    except KeyError:
        print "%s not in fruit-bowl" % selection
    

    If you want to keep your code as it is you would have to make something like the following:

    for fruit in fruit_bowl:
        i = fruit_bowl.index(fruit)
        if self.enter.get()==fruit_bowl[i][0]:
            self.texture_option.set(fruit_bowl[i][1])
            self.climate_option.set(fruit_bowl[i][2])
    

    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_bowl and assigned the value
    of 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:

    for i in xrange(len(fruit_bowl)):
        if self.enter.get()==fruit_bowl[i][0]:
            self.texture_option.set(fruit_bowl[i][1])
            self.climate_option.set(fruit_bowl[i][2])
    

    If you have further question, just post a comment and i will update my answer accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If i run this Python code: from Tkinter import *; w = Tk(); w.geometry(
Consider this Python code for printing a list of comma separated values for element
I was given this Python code that would calculate an MD5 value for any
/Users/smcho/Desktop/bracket/[10,20] directory has abc.txt, but when I run this Python code import glob import
I have the following python code... import Tkinter root = Tkinter.Tk() root.tk.eval('puts {printed by
I'm using Tkinter in Python, and trying to create code to run when a
I made this code into an executable with py2exe: # File: zipfile-example-1.py from Tkinter
This code does not give a clean exit when the Exit button is clicked.
I've written a simple GUI program in python using Tkinter. Let's call this program
I'm using Python Tkinter and I want to place a variable number of text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.