如何使用tkinter for python中的迭代创建输入字段和按钮以从输入字段获取内容? [英] how do I create entry fields and buttons to get the content from the entry field using iteration in tkinter for python?
问题描述
我正在尝试使用用户指定的一定数量的按钮和输入字段制作tkinter GUI,因此在下面的这段代码中,例如,如果用户将变量号指定为3,我希望3个输入框和,我想要对其进行设置,以便在第一个字段中键入一个值并单击其旁边的按钮时,希望该按钮从其旁边的输入字段中读取该值.我还需要将每个输入字段分配给将通过相同的迭代循环创建的变量.但是,特别是在将按钮映射到输入字段方面,我遇到了困难,因为我似乎总是遇到文本"AttributeError:'NoneType'对象没有属性'get'"的错误.任何人都可以修复我的代码或帮助我找到该问题的替代解决方案吗?抱歉,说明有些混乱. 这与仅获取入口小部件的内容的问题不同,因为我需要它使用迭代来创建一定数量的入口小部件和按钮,而我的问题已被标记为重复的问题并不能说明该问题.迭代地将每个输入字段映射到每个按钮.例如,如果我输入3作为变量,则我需要程序创建输入字段1,输入字段2和输入字段3以及按钮1,按钮2和按钮3,然后使用迭代将每个按钮映射到其各自的输入字段. 我尝试使用字典,但这似乎并没有太大帮助.
I'm trying to make a tkinter GUI with a certain amount of buttons and entry fields that is specified by the user, so in this code below for example if the user specifies the variable number to be 3 I want there to be 3 entry boxes and , and I want to set it up so that if I type a value into the first field and click the button next to it, I want that button to read the value from the entry field next to it. I also need to assign each entry field to a variable that will be created through the same iterative loop. However, I'm having difficulty especially in regards to mapping the buttons to the entry fields, as I always seem to run up against an error with the text "AttributeError: 'NoneType' object has no attribute 'get'". Would anyone be able to either fix my code or help me find an alternative solution to the problem? Sorry if the description's a bit confusing. This is different from just a problem of just getting the contents of the entry widget as I need it to create a certain amount of entry widgets and buttons using iteration, and the question that my question has been marked a duplicate of doesn't explain how to iteratively map each entry field to each button. For example, if I enter 3 as the variable, I need the program to create entry field 1, entry field 2 and entry field 3 and button 1, button 2 and button 3 and then map each button to its respective entry field using iteration. I've tried using dictionaries, but this doesn't seem to help much.
import tkinter as tk
root = tk.Tk()
number = 3
d={}
def callBack():
print(d["E{0}".format(i)].get())
return
for i in range(0,number):
d["E{0}".format(i)] = tk.Entry(root)
d["E{0}".format(i)].grid(row=i, column=0)
d["B{0}".format(i)] = tk.Button(root, text="test", command=callBack)
d["B{0}".format(i)].grid(row=i, column=1)
推荐答案
在调用grid
之前,您需要保存Entry
和Button
:
You need to save the Entry
and Button
before calling grid
:
import tkinter as tk
number = 3
root = tk.Tk()
def get_on_click(widget_dict, entry_name):
def on_click():
result = widget_dict[entry_name].get()
print("%s = %s" % (entry_name, result))
return result
return on_click
d = dict()
for i in range(0, number):
entry_name = "E{0}".format(i)
button_name = "B{0}".format(i)
print(entry_name, button_name)
d[entry_name] = tk.Entry(root)
d[entry_name].grid(row=i, column=0)
d[button_name] = tk.Button(root, text="test", command=get_on_click(d, entry_name))
d[button_name].grid(row=i, column=1)
root.mainloop()
这应该可以帮助您入门.
This should help you get started.
在评论中,您询问如何将值保存在Entry
中.我将创建一个类来处理所有事情:
In your comment, you ask how to save the value in the Entry
. I would create a class to handle everything:
import tkinter as tk
number = 3
root = tk.Tk()
class EntryButton(object):
def __init__(self, root, number):
self.number = number
self.entry = tk.Entry(root)
self.button = tk.Button(root, text="test", command=self.on_click)
self.entry.grid(row=number, column=0)
self.button.grid(row=number, column=1)
self.value = None
def on_click(self):
self.value = self.entry.get()
storage = dict()
for i in range(0, number):
storage[i] = EntryButton(root, i)
root.mainloop()
for i in range(0, number):
value = storage[i].value
print(f"storage[{i}] = {value}")
如您所见,这消除了很多额外的工作.
As you can see, this eliminates a lot of extra work.
这篇关于如何使用tkinter for python中的迭代创建输入字段和按钮以从输入字段获取内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!