Wesley Meyer
Wesley Meyer

Reputation: 15

Looking for cleaner way to write this Tkinter code using looping

I am developing a GUI using the Tkinter module in Python 3.6. The code is becoming messy as the GUI is developing. I believe if I learn how to use loops to create Label() lists and loops to .grid(),.pack() or .place() them into the frames, this code will be easier to read.

I am playing around with .grid() and .pack() to put the information into my frames and haven't figured out how to cleanly organize the labels.

And advise on how to make this easier to read, particularly with the labels up the code below would help. Thanks.

import tkinter as tk


root = tk.Tk()

# Main window height
HEIGHT = 500  # pixels
WIDTH = 1200  # pixels

canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

frame = tk.Frame(root, bg='#DCDCDC')
frame.place(relx=0.01, rely=0.025, relwidth=0.98, relheight=0.95)

title = tk.Label(frame, text="MUTCD Compliant Street Sign Calculator", bg='#008000', fg="white", bd=1, relief='ridge')
title.place(relx=.2, rely=.02, relwidth=.6)

# Fields to organize widgets
input_frame_master = tk.Frame(frame, bg='#C6C6C6', bd=1, relief='ridge')
input_frame_master.place(rely=0.1, relx=0.02, relwidth=.4, relheight=.85)

input_frame_child = tk.Frame(input_frame_master, bg="#C6C6C6")
input_frame_child.place(relx=0.05, rely=.15, height=200, relwidth=.3)

#Not used for this question
input_frame_child2 = tk.Frame(input_frame_master, bg="red")
input_frame_child2.place(relx=.35, rely=.15, relwidth=.40, height=200)

input_frame_child3 = tk.Frame(input_frame_master, bg="blue")
input_frame_child3.place(relx=.75, rely=.15, relwidth=.17, height=200)

# Input frame master
input_frame_title = tk.Label(input_frame_master, text="User Input", bg='#C6C6C6')
input_frame_title.place(relx=.05, rely=0.05, relwidth=.9)

# Input descriptions into frame child 1
sname_m = tk.Label(input_frame_child, text="Sign Name: ", bg='#C6C6C6')
sname_m.grid(row=0, column=0)

desg_m = tk.Label(input_frame_child, text="Designation: ", bg='#C6C6C6')
desg_m.grid(row=1, column=0)

arw_m = tk.Label(input_frame_child, text="Arrow: ", bg='#C6C6C6')
arw_m.grid(row=2, column=0)

alp_m = tk.Label(input_frame_child, text="Alphabet: ", bg='#C6C6C6')
alp_m.grid(row=3, column=0)

ilth_m = tk.Label(input_frame_child, text="Initial Letter Text Height: ", bg='#C6C6C6')
ilth_m.grid(row=4, column=0)

dsth_m = tk.Label(input_frame_child, text="Lower Case Text Height: ", bg='#C6C6C6')
dsth_m.grid(row=5, column=0)

es_m = tk.Label(input_frame_child, text="Edge Spacing: ", bg='#C6C6C6')
es_m.grid(row=6, column=0)

sname_m = tk.Label(input_frame_child, text="Word Spacing: ", bg='#C6C6C6')
sname_m.grid(row=7, column=0)

#Input tags into frame child 3
sname_ind = tk.Label(input_frame_child3, text=" ", bg='#C6C6C6', height=2)
sname_ind.pack(fill='x', anchor='w')

desg_ind = tk.Label(input_frame_child3, text="(St, Ave, Etc.)", bg='#C6C6C6', height=2)
desg_ind.pack(fill='x', anchor='w')

arw_ind = tk.Label(input_frame_child3, text="(Street arrow)", bg='#C6C6C6')
arw_ind.pack(fill='x', anchor='w')

alp_ind = tk.Label(input_frame_child3, text=" ", bg='#C6C6C6')
alp_ind.pack(fill='x', anchor='w')

ilth_ind = tk.Label(input_frame_child3, text="Inches ", bg='#C6C6C6')
ilth_ind.pack(fill='x', anchor='w')

dsth_ind = tk.Label(input_frame_child3, text="Inches", bg='#C6C6C6')
dsth_ind.pack(fill='x', anchor='w')

es_ind = tk.Label(input_frame_child3, text="Inches", bg='#C6C6C6')
es_ind.pack(fill='x', anchor='w')

sname_ind = tk.Label(input_frame_child3, text="Inches", bg='#C6C6C6')
sname_ind.pack(fill='x', anchor='w')

root.mainloop()

Upvotes: 1

Views: 180

Answers (1)

Novel
Novel

Reputation: 13729

Maybe I'm missing something about your question, but you can just stick tkinter code in a normal loop just like any other code.

# Input descriptions into frame child 1
labels = ("Sign Name: ","Designation: ","Arrow: ", "Alphabet: ","Initial Letter Text Height: ","Lower Case Text Height: ","Edge Spacing: ", "Word Spacing: ")
for row_num, label_text in enumerate(labels):
    lbl = tk.Label(input_frame_child, text=label_text, bg='#C6C6C6')
    lbl.grid(row=row_num, column=0)


#Input tags into frame child 3
labels = (" ","(St, Ave, Etc.)","(Street arrow)"," ","Inches ","Inches ","Inches ","Inches ")
for label_text in labels:
    lbl = tk.Label(input_frame_child3, text=label_text, bg='#C6C6C6')
    lbl.pack(fill='x', anchor='w')

Upvotes: 1

Related Questions