我能以某种方式使用for循环吗?

问题描述 投票:-1回答:1

只是想知道是否有一种方法来清理这一点,其中一堆线只变化了一点。这是使用python 3.4.3,tkinter和mysql.connector。

    Plantname = tk.StringVar()
    self.Plantbox = tk.Entry(self, textvariable=Plantname)
    self.Plantbox.grid(row=0, column=0)

    self.Name = tk.Label(self, text="Name",width=10)
    self.Name.grid(row=1, column=0)

    self.Amount = tk.Label(self, text="Amount",width=10)
    self.Amount.grid(row=1, column=1)

    self.Date = tk.Label(self, text="Date",width=10)
    self.Date.grid(row=1, column=2)

    self.Planting = tk.Label(self, text="Planting #",width=10)
    self.Planting.grid(row=1, column=3)

    self.batch = tk.Label(self, text="batch #",width=10)
    self.batch.grid(row=1, column=4)

    self.Name_2 = tk.Label(self, text="0")
    self.Name_2.grid(row=2, column=0)

    self.Amount_2 = tk.Label(self, text="0")
    self.Amount_2.grid(row=2, column=1)

    self.Date_2 = tk.Label(self, text="0")
    self.Date_2.grid(row=2, column=2)

完整代码:

https://pastebin.com/JPjrtdEg

python tkinter
1个回答
1
投票

是的,你可以使用一个循环。 tkinter对象没有什么特别之处,使得它们与任何其他python对象有任何不同。

for col, heading in enumerate(("Name", "Amount", "Date", "Planting #", "batch #")):
    label = tk.Label(self, text=heading, width=10)
    label.grid(row=1, column=col)
© www.soinside.com 2019 - 2024. All rights reserved.