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 8146101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:52:50+00:00 2026-06-06T13:52:50+00:00

I have the following code (only sections of it) for getting the entries in

  • 0

I have the following code (only sections of it) for getting the entries in 4 Entry boxes I have created. However I have two niggles:

  • When I type into each box, it types the same thing and I wish to type different numbers and assign all of them to separate variables.

  • Is there any way of producing 4 boxes in a loop and fewer lines than this?

    number = StringVar()
    def numberwritten(*args):
    number.trace(“w”, numberwritten)
    fg = number.get()
    print fg

    In separate definition def ChoiceBox(choice): (not full code under this def)

    def ChoiceBox(choice):
    i = [0, 1, 2, 3]

        for i in i:
            choice_title = Label(choice_frame, text='Value %g'% float(i+1), bg='white', borderwidth=0, width=0)
            choice_title.grid(row=0, column=column+i, sticky="nsew", padx=1, pady=1)
    
        box1 = Entry(choice_frame, bg='white', borderwidth=0, width=0, textvariable=number)
        box1.grid(row=1, column=0, sticky="ew", padx=1, pady=1)
        box2 = Entry(choice_frame, bg='white', borderwidth=0, width=0, textvariable=number)
        box2.grid(row=1, column=1, sticky="ew", padx=1, pady=1)
        box3 = Entry(choice_frame, bg='white', borderwidth=0, width=0, textvariable=number)
        box3.grid(row=1, column=2, sticky="ew", padx=1, pady=1)
        box4 = Entry(choice_frame, bg='white', borderwidth=0, width=0, textvariable=number)
        box4.grid(row=1, column=3, sticky="ew", padx=1, pady=1)
    

UPDATE/EDIT:

This is the section of code I have and cannot figure out what’s going wrong with it at the end as I’m receiving syntax errors:

def numberwritten(number):
    fg = number.get()
    print fg

numbers = [StringVar() for i in xrange(4) ] #Name available in global scope. 
for i in numbers: 
    i.trace('w',lambda n=i: numberwritten(n) ) 


def ChoiceBox(choice):


    column = 0
    if choice == "Fixed":
        choice_frame.grid_forget()      
    tkMessageBox.showinfo("Message", "No optimisation, value fixed.")
    elif choice == "List":

        for i in xrange(4): 
            choice_title = Label(choice_frame, text='Value %g'% float(i+1), bg='white', borderwidth=0, width=0) 
        choice_title.grid(row=0, column=column+i, sticky="nsew", padx=1, pady=1) 
        boxes=[] 

        tkMessageBox.showinfo("Message", "Please fill in list values.")


    elif choice == "Interval" or "Optimisation":
        i = [0, 1]
        choice_title1 = Label(choice_frame, text='Min Value', bg='white', borderwidth=0, width=0)
        choice_title1.grid(row=0, column=column, sticky="N S E W", padx=1, pady=1)
        choice_title2 = Label(choice_frame, text='Max Value', bg='white', borderwidth=0, width=0)
        choice_title2.grid(row=0, column=column+1, sticky="nsew", padx=1, pady=1)

        boxes=[]

        tkMessageBox.showinfo("Message", "Enter Min/Max values.")

for i in xrange(4): 
        box=Entry(choice_frame,bg='white',borderwidth=0,textvariable=numbers[i]) 
        box.grid(row=1,column=i, sticky='ew', padx=1, pady=1 
        boxes.append(box)
    box1,box2,box3,box4=boxes
  • 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-06T13:52:52+00:00Added an answer on June 6, 2026 at 1:52 pm

    First, please stop using:

    i=[1,2,3,4]
    for i in i:
        ...
    

    Use:

    for i in (1,2,3,4):
        ...
    

    instead.

    Or better:

    for i in xrange(1,5): #range in python3.x
        ...
    

    Now, creating the stuff in the loop is easy:

    strvars=[]
    boxes=[]
    for i in xrange(1,5):
        svar=StringVar()
        box=Entry(choice_frame, bg='white', borderwidth=0, width=0, textvariable=svar)
        box.grid(row=1, column=i-1, sticky="ew", padx=1, pady=1)
        svar.trace('w',numberwritten)
        #If you want to pass the box associated with the stringvar, you can do this:
        #svar.trace('w',lambda a,b,c,box=box : numberwritten(box)) #box.get() get's the contents of an Entry too!
        strvars.append(svar)
        boxes.append(box)
    
    number1,number2,number3,number4=strvars  #unpack stringvars 
    box1,box2,box3,box4=boxes   #unpack boxes
    

    You need a distinct StringVar for each box. The unpacking only works if you know how many items there are in the lists holding the StringVars and Entry boxes. Otherwise, you can get references to the box/variable you want by boxes[0] and strvars[0] for example.

    EDIT

    Something like this should work…

    def numberwritten(number):
        fg = number.get()
        print fg 
    
    numbers = [StringVar() for i in xrange(4) ]  #Name available in global scope.
    for i in numbers:
        i.trace('w',lambda a,b,c,n=i: numberwritten(n) )
    
    # In separate definition def ChoiceBox(choice): (not full code under this def)
    def ChoiceBox(choice):  
    
        for i in xrange(4):
            choice_title = Label(choice_frame, text='Value %g'% float(i+1), bg='white', borderwidth=0, width=0)
            choice_title.grid(row=0, column=column+i, sticky="nsew", padx=1, pady=1)
    
        boxes=[]
        for i in xrange(4):
            box=Entry(choice_frame,bg='white',borderwidth=0,textvariable=numbers[i])
            box.grid(row=1,column=i, sticky='ew', padx=1, pady=1
            boxes.append(box)
    
        box1,box2,box3,box4=boxes
    

    As an aside, why are you using width=0 everywhere? an Entry/Label with no width is pretty much useless.

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

Sidebar

Related Questions

I have the following scenario. The client code only has access to FooHandler, not
I have written the following code. But it is removing only &nbsp; not <br>
I have multiple messages in SQS. The following code always returns only one, even
In following code, I have one large component, and I'd like only the level4
How do I change the following bit of code so that I only have
I have following code in my application: // to set tip - photo in
I have following code in my Application. Comments in my code will specify My
I have following code in my application. [self.navigationController pushViewController:x animated:YES]; It will push a
I have following code class User attr_accessor :name end u = User.new u.name =
i have following code to show div on page <div id=all_users> <div id=user11 userid=11

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.