gpt4 book ai didi

python - 列表中的 Tkinter Entry 小部件都保存相同的数据

转载 作者:行者123 更新时间:2023-11-30 22:32:25 24 4
gpt4 key购买 nike

我有一个名为 display_change 的函数,它位于我的 tkinter GUI 中间。我希望用户选择一个数字并创建他们选择的输入框的数量。我想出了如何使用 for 循环来做到这一点,并将每个输入框放入一个列表中。然而,每当我写一些东西时,所有条目小部件之间都会保存相同的数据。

我不想显示我的所有代码,但可以显示发生这种情况的整个函数。

import tkinter as tk
from tkinter.ttk import Frame, Button
labels = []
entries = []

class Application(Frame):
def __init__(self)
super().__init__()
self.mainFrame()

def mainFrame(self):
self.master.title("Setup")
self.pack(fill=tk.BOTH, expand=True)
self.columnconfigure(1, weight=1)
self.columnconfigure(3, pad=7)
self.rowconfigure(3, weight=1)
self.rowconfigure(5, pad=7)
lbl = tk.Label(self, text="Follow the instructions on each page", bg="snow2")
lbl.grid(sticky=tk.W, pady=4, padx=5)

area = tk.Frame(self, bg="white")
area.grid(row=1, column=0, columnspan=3, rowspan=4,
padx=5, sticky=tk.E + tk.W + tk.S + tk.N)

# ----------Inside White Box ---------------------
lbl = tk.Label(area, text="Select the number of parts to create:")
lbl.grid(row=1, column=0)

choices = {0, 3, 4, 5, 6, 7, 8, 9, 10}

node_count = tk.IntVar()
node_count.set(0)
node_select = tk.OptionMenu(area, node_count, *choices,
command=lambda x: self.display_change(area, node_count.get()))
node_select.grid(row=1, column=2)

# -----------Outside Part-------------------------
abtn = Button(self, text="Thing 1")
abtn.grid(row=1, column=3, sticky=tk.W)
cbtn = Button(self, text="Thing 2")
cbtn.grid(row=2, column=3, pady=4, sticky=tk.W)

abtn2 = Button(self, text="Thing 3")
abtn2.grid(row=3, column=3, sticky=tk.W + tk.N)

cbtn2 = Button(self, text="Thing 4")
cbtn2.grid(row=3, column=3, pady=28, sticky=tk.W + tk.N)

hbtn = Button(self, text="Exit")
hbtn.grid(row=5, column=2, sticky=tk.W)

sbtn = Button(self, text="Save")
sbtn.grid(row=5, column=3, pady=3, sticky=tk.W)

sbtn = Button(self, text="Help")
sbtn.grid(row=5, column=0, sticky=tk.W)

def display_change(self, area, nodes):
"""Here is where the display is changed so what the user choose is correctly displayed"""

lower_label = tk.Label(area, text="Enter the value of each part")
lower_label.grid(row=2, column=0, sticky=tk.N + tk.W)

global labels, entries
for label in labels:
label.destroy()
for entry in entries:
entry.destroy()
labels = []
entries = []

# This loop creates the correct number of entry box's and labels. Each entry is stored separately
for i in range(nodes):
if nodes <= 4:
labels.append(tk.Label(area, text="Part "+str(i+1)))
labels[i].place(x=10+(120*i), y=55)
entries.append(tk.Entry(area, text="Change"))
entries[i].place(x=10 + (120 * i), y=80, width=100)
else:
labels.append(tk.Label(area, text="part " + str(i + 1)))
labels[i].place(x=10 + (120 * i), y=105)
entries.append(tk.Entry(area, text="Change"))
entries[i].place(x=10 + (120 * i), y=160, width=100)

if __name__ == "__main__":
root = tk.Tk()

"""Calculate center of screen so popup is center"""
w = 650
h = 400

ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()

x = (ws / 2) - (w / 2)
y = (hs / 2) - (h / 2)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))

# This line prevents the user from changing the size of the window
root.resizable(width=False, height=False)

app = Application(root)
root.mainloop()

这就是我输入内容时的样子:

我只输入了一次“abcd”,但它进入了所有条目

最佳答案

当您查看所有可用的config时tkinters 文档中的选项 输入字段 text 不是其中之一。很可能是因为 textvariable 是 Entry 字段的可用参数,tkinter 将 text 视为 textvariable 的缩写。因此,结果是您的所有条目字段都被分配了相同的文本变量,并且当一个条目更改该变量时,所有条目都会使用该变量中的新字符串进行更新。

编写 for 循环的正确方法如下所示:

for i in range(nodes):
if nodes <= 4:
labels.append(tk.Label(area, text="Part {}".format(i+1)))
labels[i].place(x=10+(120*i), y=55)
entries.append(tk.Entry(area))
entries[i].insert(0, "Change Ip") # adds text directly to an entry field
entries[i].place(x=10 + (120 * i), y=80, width=100)
else:
labels.append(tk.Label(area, text="Node {}".format(i+1)))
labels[i].place(x=10 + (120 * i), y=105)
entries.append(tk.Entry(area)
entries[i].insert(0, "Change Ip")
entries[i].place(x=10 + (120 * i), y=160, width=100)

关于python - 列表中的 Tkinter Entry 小部件都保存相同的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45463841/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com