0

Ok, my code below will be very obvious on what im trying to do.

I get the "cant assign operator" error and im unsure a work around and i would much rather do this in a loop.

I need to make the loop make the variable then grid it using Tkinter.

I want the name of the widget to change upon every loop so hopefully it should make many different widgets with different variable names. for example: wall + loop + label = tk.Label() I want that to do the equivalent to wall1label = tk.Label() and wall2label = tk.Label() etc

Heres my code. Answer with any useful information you need.

for loop in range(1, 4):

    wall + loop + Label = tk.Label(root, text="Wall " + str(loop)).grid(row=int(loop+1), column=int(loop-1), pady=5, padx=5)
    wall + loop + w = tk.Label(root, text="Width").grid(row=int(loop+1), column=int(loop-1), pady=5, padx=5)
    wall + loop + wE = tk.Entry(root).grid(row=int(loop+1), column=int(loop-1), pady=5, padx=5)
    wall + loop + h = tk.Label(root, text="Height").grid(row=int(loop+1), column=int(loop-1), pady=5, padx=5)
    wall + loop + hE = tk.Entry(root).grid(row=int(loop+1), column=int(loop-1), pady=5, padx=5)
    wall + loop + ww = tk.Label(root, text="Window/Door Width").grid(row=int(loop+1), column=int(loop-1), pady=5, padx=5)
    wall + loop + wwE = tk.Entry(root).grid(row=int(loop+1), column=int(loop-1), pady=5, padx=5)
    wall + loop + wh = tk.Label(root, text="Window/Door Height").grid(row=int(loop+1), column=int(loop-1), pady=5, padx=5)
    wall + loop + whE = tk.Entry(root).grid(row=int(loop+1), column=int(loop-1), pady=5, padx=5)
    wall + loop + bottombreak = tk.Label(root, text="").grid(row=int(loop+1), column=int(loop-1), pady=5, padx=5)
Jay Parker
  • 3
  • 1
  • 3
  • Sorry, your code isn't obvious for me. Is the `wall + loop + ` part meant to change the variable name for the widget? If so, have you thought about using a dictionary? – FamousJameous May 19 '17 at 21:04
  • Can you give me an example of using a dictionary? I'm not really sure it would make any difference or need to do what i need it to do. – Jay Parker May 19 '17 at 21:09
  • Assuming your answer to my first question was yes, you create the dictionary keys as strings which avoids the exception you are seeing. Ex. `mywidgets['{}_{}_Label'.format(wall, loop)] = tk.Label(...)`. If you want the 10 lines inside of your existing loop to be replaced by another loop, you will have to create a data structure to define what gets created by that loop. You should probably clarify that in your question. – FamousJameous May 19 '17 at 21:16
  • are you trying to use `wall + loop + w` as a variable name? Otherwise why do you have an addition on the left side of an assignment? (not obvious what you are trying to do to me either) – Tadhg McDonald-Jensen May 19 '17 at 21:16
  • 1
    Are you aware that `.grid()` returns `None` so you'd end up with all your variables equal to `None` either way unless you did `.grid` as a seperate operation. – Tadhg McDonald-Jensen May 19 '17 at 21:17
  • I've found a way around it, thanks for all replies – Jay Parker May 19 '17 at 21:19

0 Answers0