- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个名为 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/
Eclipse 提示此代码“类型参数 Entry 隐藏了类型 Map.Entry”: import java.util.Map.Entry; public class Test { stat
这两个for语句是等价的。 import java.util.Map; import java.util.Map.Entry; for (Map.Entry entry: map.entrySet()
我想在 python 字典中添加一个键/值,它依赖于现有的键/值。示例 x = {} x["length"] = 12 x["volume"] =x["lenght"] * 10 行得通;但是有没有可
我使用 NuGet 将我的 EntityModel 升级到版本 4.3。 现在我想更改我的 EntityObject.State,但找不到 .Entry() 方法。 当前状态是已删除。 这就是我想要做
我是 Java 新手,正在使用 HashMap 在 Mac 上编写 Java。 但是我遇到了一个问题,找不到答案 import java.util.Map; import java.util.Hash
我正在切换一个小应用程序(Win 7/64 上的 Python 2.7.3/32)来使用 ttk,但我在使 ttk.Entry 按照 tk.Entry 的方式工作时遇到问题;当我设置其内容时,ttk.
为什么我需要在 i.next(); 前面加上 (Map.Entry) ?为什么我不能只有 Map.Entry m = i.next();? 对不起。它是一个 HashMap。 最佳答案 因为它显然不是
我的 xPage SSJS 失败: viewEntry = view.getNext(viewEntry); 有错误 Notes error: Entry not found in index 我确实
我正在使用 DataTable在我的申请中。 我想隐藏左下角的细节,我该怎么做? “显示 1,657 个条目中的 1 到 10 个(从 9,044 个条目中筛选出来)” 这是我的设置: $('#inv
我有两列,一个时间戳和一个已选择卡片的数字。 card added 1 2016-09-23 13:48:48 3 2016-09-23 13:48:48 1
我正在使用 Entity Framework 4.1,并且我有我的 DbContext Override SaveChanges 来审核属性更改。从“GetEntryValueInString”返回空
我正在使用 Tkinter 在 Python 3 上编写 GUI,但每次使用 Entry() 时,我都会收到名称错误。 我尝试了一个更简单的代码版本,(写在下面),但它仍然导致了 NameError:
我刚刚创建了一个插入方法来对数组进行排序,这是我在该方法中完成的代码; public static void insertionSort (Entry[] array2){ for (int
使用这个例子1对于 n:n 关系: (来源:tekstenuitleg.net) 设置主要或主要多对多字段的最佳方法是什么?示例:假设我想将经销商“Devrolijke drinker”(ID AB9
是否可以使用 Entry 通过 AsRef 获取值的 API , 但用 Into 插入它? 这是工作示例: use std::collections::hash_map::{Entry, HashMa
是否可以使用 Entry 通过 AsRef 获取值的 API , 但用 Into 插入它? 这是工作示例: use std::collections::hash_map::{Entry, HashMa
是否可以使用 Entry 通过 AsRef 获取值的 API , 但用 Into 插入它? 这是工作示例: use std::collections::hash_map::{Entry, HashMa
表定义: CREATE TABLE PositionalDataNGS ( Date DATE, Time TIME(3) , X FLOAT(5), Y FLOAT(5), D FLOAT(5) ,
我的平台: PHP 和 MySQL 我这里有什么: 我有 4 个表,分别是“books”、“book_type”、“book_categories”、“all_categories”。 我想做什么:
I am using MySQL 5.1.56, MyISAM. My table looks like this:我使用的是MySQL 5.1.56,MyISAM。我的桌子是这样的: CR
我是一名优秀的程序员,十分优秀!