python - 寻找使用循环编写此 Tkinter 代码的更简洁方法
问题描述
我正在使用 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()
解决方案
也许我错过了关于您的问题的一些内容,但是您可以像任何其他代码一样将 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')
推荐阅读
- react-native - 使用 FlatList 渲染项目时在哪里放置逻辑
- c# - 自定义 IEqualityComparer 使用 LINQ 计算两个列表之间的差异,除了
- python - Jinja 模板 - 从表格列中删除重复项
- .net - 线程使用量增加——找到需求在哪里
- flutter - 如何更改 Flutter web 中的 url?
- angular - Angular 可以使用托管服务帐户搜索活动目录(不是 Azure)吗
- python-3.x - 在 Python 中使用静态 IP 和端口访问 IP 摄像机
- python - 如何将熊猫数据框中的单列转换为多个新列?
- r - 从命名列表中选择一列
- json - 当作为对象传入时,如何获取 ASP.NET MVC 中模型对象的属性值?