检查代码错误并修改 def create_rul(self): # 村庄数目输入框 self.num_label = tk.Label(self, text="村庄数目") self.num_label.pack() self.num_entry = tk.Entry(self) self.num_entry.pack() # 可建公路数目输入框 self.edge_label = tk.Label(self, text="可建公路数目") self.edge_label.pack() self.edge_entry = tk.Entry(self) self.edge_entry.pack() # 确认按钮 self.confirm_button = tk.Button(self, text="确认", command=self.create_widgets) self.confirm_button.pack() def create_widgets(self): # 获取村庄数目和可建公路数目 num = int(self.num_entry.get()) edge = int(self.edge_entry.get()) # 村庄名称输入框 self.village_label = tk.Label(self, text="村庄名称") self.village_label.pack() for i in range(num): self.village_entry = tk.Entry(self) self.village_entry.pack() # 村村连同道路信息输入框 self.road_label = tk.Label(self, text="村村连同道路信息‘村,村,路’") self.road_label.pack() for j in range(edge): self.road_entry = tk.Entry(self) self.road_entry.pack() # 提交按钮 self.submit_button = tk.Button(self, text="确认", command=self.submit) self.submit_button.pack()
时间: 2024-02-14 22:04:43 浏览: 104
基于ANN人工神经网络的锂电池寿命预测(Remaining Useful Life,RUL)(Python完整源码和数据)
5星 · 资源好评率100%
在 `create_widgets` 函数中,每次循环创建 `self.village_entry` 和 `self.road_entry` 时,应该使用列表将它们存储起来,否则后面的循环会覆盖前面的输入框,导致只有最后一个输入框被保留。同时,在 `submit` 回调函数中,应该获取所有输入框的值,而不是只获取最后一个输入框的值。具体修改如下:
```
def create_rul(self):
# 村庄数目输入框
self.num_label = tk.Label(self, text="村庄数目")
self.num_label.pack()
self.num_entry = tk.Entry(self)
self.num_entry.pack()
# 可建公路数目输入框
self.edge_label = tk.Label(self, text="可建公路数目")
self.edge_label.pack()
self.edge_entry = tk.Entry(self)
self.edge_entry.pack()
# 确认按钮
self.confirm_button = tk.Button(self, text="确认", command=self.create_widgets)
self.confirm_button.pack()
def create_widgets(self):
# 获取村庄数目和可建公路数目
num = int(self.num_entry.get())
edge = int(self.edge_entry.get())
# 村庄名称输入框
self.village_label = tk.Label(self, text="村庄名称")
self.village_label.pack()
self.village_entries = [] # 存储村庄名称输入框
for i in range(num):
village_entry = tk.Entry(self)
village_entry.pack()
self.village_entries.append(village_entry)
# 村村连同道路信息输入框
self.road_label = tk.Label(self, text="村村连同道路信息‘村,村,路’")
self.road_label.pack()
self.road_entries = [] # 存储村村连同道路信息输入框
for j in range(edge):
road_entry = tk.Entry(self)
road_entry.pack()
self.road_entries.append(road_entry)
# 提交按钮
self.submit_button = tk.Button(self, text="确认", command=self.submit)
self.submit_button.pack()
def submit(self):
# 获取所有输入框的值
villages = [village_entry.get() for village_entry in self.village_entries]
roads = [road_entry.get() for road_entry in self.road_entries]
# 处理输入数据
...
```
这样,每个输入框都会被存储在列表中,便于后续获取它们的值。
阅读全文