寻找更干净的方法来使用循环编写此Tkinter代码

时间:2019-12-18 03:57:25

标签: python tkinter python-3.6

我正在使用Python 3.6中的Tkinter模块开发GUI。随着GUI的开发,代码变得越来越混乱。我相信,如果我学习如何使用循环创建Label()列表并将循环到.grid()、. pack()或.place()到框架中,则此代码将更易于阅读。

我正在使用.grid()和.pack()将信息放入框架中,但还没有弄清楚如何整齐地组织标签。

并建议如何使它更易于阅读,尤其是在下面的代码上贴上标签会有所帮助。谢谢。

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()

1 个答案:

答案 0 :(得分:1)

也许我缺少有关您的问题的信息,但是您可以像其他任何代码一样,将tkinter代码粘贴在普通循环中。

# 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')