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

  • SEARCH
  • Home
  • 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 8208169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:14:54+00:00 2026-06-07T09:14:54+00:00

I have the following code where I ask the user to open a text

  • 0

I have the following code where I ask the user to open a text which can include several rows within a section of data.

I then require for each row an OptionMenu to be created. My problem is that I need to do different things for each option menu and apply it only to that specific row in the ‘grid’. I cannot do this as I’m creating them all under the same name and don’t understand how to do differently.

with askopenfile(filetypes=[(".txt files","*.txt")], title='Import', mode='r') as f: 
    data_dict=parse_file(f) 


info=data_dict['three'] 
i = 2
for row in info:
    # Create row in 'table' for each output
    no_1, code, value = row             # Obtain results
    def three( code ):
        c = { "1" : "1",
              "2" : "2",
              "3" : "3" }
        try:
            return c[code]
        except KeyError:
            return "None"


    variablelist = StringVar(self.frame_table)
    variablelist.set("Fixed")
    self.list1 = OptionMenu(self.frame_table, variablelist, "Fixed", "List", "Min", "Max", command=self.ChoiceBox)
    self.list1.grid(row=i, column=6, sticky="nsew", padx=1, pady=1)
    i = i + 1

For example, I would like to create an extra box next to the second row out of three but as the third row is created last, when I try to obtain the grid info, I can only add it onto the third row.

My reasoning for having done it like this is because I don’t always know the amount of rows and I didn’t want to create lots of lines of code creating an option menu separately each time (even if I did know the number of rows).

  • 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-06-07T09:14:56+00:00Added an answer on June 7, 2026 at 9:14 am

    Your question is very unclear, mostly because your example doesn’t make any sense. Why, for example, are you redefining the function three on every iteration, yet you never use it?

    Regardless, maybe the following example will help you. It creates three option menus, associates a variable with each, and has each of them call the same procedure when the value changes. By default this method will be given the new value of the option menu; by using a lambda function we can also pass the row number to the function.

    import Tkinter as tk
    
    # data for the example
    info = (
        ("1.1","1.2","1.3"),
        ("2.1","2.2","2,3"),
        ("3.1","3.2","3.3"),
    )
    
    class SampleApp(tk.Tk):
        def __init__(self):
            tk.Tk.__init__(self)
            self.status = tk.Label(self, anchor="w")
            self.frame_table = tk.Frame(self, background="black")
            self.frame_table.grid_columnconfigure(1, weight=1)
            self.frame_table.grid_columnconfigure(2, weight=1)
            self.frame_table.grid_columnconfigure(3, weight=1)
    
            self.status.pack(side="bottom", fill="x")
            self.frame_table.pack(side="top", fill="x", padx=10, pady=10)
    
            for row_number, row_data in enumerate(info):
                no_1, code, value = row_data
                label = tk.Label(self.frame_table, text="row %d" % row_number, background="gray")
                col1  = tk.Label(self.frame_table, text=no_1)
                col2  = tk.Label(self.frame_table, text=code)
                col3  = tk.Label(self.frame_table, text=value)
    
                # N.B. 'value' is automatically passed to
                # the command by tkinter; we're adding
                # an extra parameter, 'row' 
                command=lambda value, row=row_number: self.ChoiceBox(value, row)
                stringvar = tk.StringVar(self.frame_table)
                stringvar.set("Fixed")
                option_menu = tk.OptionMenu(self.frame_table, stringvar, 
                                            "Fixed","List","Min","Max", 
                                            command=command)
                label.grid(row=row_number, column=0, sticky="nsew", padx=1, pady=1)
                col1.grid(row=row_number, column=1, sticky="nsew", padx=1, pady=1)
                col2.grid(row=row_number, column=2, sticky="nsew", padx=1, pady=1)
                col3.grid(row=row_number, column=3, sticky="nsew", padx=1, pady=1)
                option_menu.grid(row=row_number, column=6, sticky="nsew", 
                                 padx=1, pady=1)
    
        def ChoiceBox(self, value, row_number):
            self.status.config(text="you set row %d to '%s'" % (row_number, value))
    
    app = SampleApp()
    app.mainloop()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which I'm using to get the user to input
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have following code for inserting data into database using PDO. It inserts data
I have following code: def whatever(url, data=None): req = urllib2.Request(url) res = urllib2.urlopen(req, data)
I am using the following code: while (invalidInput) { // ask the user to
I currently have the following code that retrieves data from the database and then
I have the following code which seems to work OK except for a minor
I have the following script to ask the user to confirm clicking a button
i have done the following code in order to fill a tuple with user
I have the following code: #include <iostream> #include <string> #include <unistd.h> using namespace std;

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.