开发者社区> 问答> 正文

寻找更干净的方式来编写这个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()

问题来源StackOverflow 地址:/questions/59385278/looking-for-cleaner-way-to-write-this-tkinter-code-using-looping

展开
收起
kun坤 2019-12-26 10:46:34 456 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')
    
    2019-12-26 10:46:57
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
《0代码搭应用》 立即下载
不止代码 立即下载
用RxSwift写易维护易读的愉悦代码 立即下载