Python 在用户输入后,在重复函数中保存条目的值
嗨,我叫克里斯,我的代码有点乱 也是我第一次问问题。所以我的问题是我有 一个按钮,每次按下时将运行一个功能,此按钮 函数在用户所在位置的旁边创建3个条目 希望在每个输入框中输入适当的信息 问题是我增加了一个按钮,当按下按钮时,应该会拉动按钮 值,并将其保存到稍后我想保存的位置 用它来做方程式。我的问题是,既然我在重复 条目的,他们不是唯一的每一次,而是一个克隆,所以我不能 似乎告诉程序查找每个条目,因为它们是 彼此,或者至少我是这么想的 我尝试了许多不同的方法来保存这些值,下面是每种方法 我尝试过的方法: 下面是我的代码中没有代码的部分,我尝试保存 价值: 如果您需要我乐意帮助的任何其他信息,我还有一个 GitHub如果需要的话,我也可以在必要时提供我的全部代码, 我正在使用pycharmPython 在用户输入后,在重复函数中保存条目的值,python,tkinter,Python,Tkinter,嗨,我叫克里斯,我的代码有点乱 也是我第一次问问题。所以我的问题是我有 一个按钮,每次按下时将运行一个功能,此按钮 函数在用户所在位置的旁边创建3个条目 希望在每个输入框中输入适当的信息 问题是我增加了一个按钮,当按下按钮时,应该会拉动按钮 值,并将其保存到稍后我想保存的位置 用它来做方程式。我的问题是,既然我在重复 条目的,他们不是唯一的每一次,而是一个克隆,所以我不能 似乎告诉程序查找每个条目,因为它们是 彼此,或者至少我是这么想的 我尝试了许多不同的方法来保存这些值,下面是每种方法 我尝试
我添加这些只是为了让你知道该做什么,或者该做什么,这只是如何继续的方法之一 基本上,只需在
调用中创建两个空列表,然后将这些条目小部件附加到前面创建的列表中,如:
def __init__(self):
.....#same bunch of code
self.lst1 = []
self.lst2 = []
然后在new_row()
方法的末尾,说:
def new_row():
....#same bunch of codes
self.lst1.append(entry_Box)
self.lst2.append(entry_Box2)
现在通过这种方式访问条目小部件,比如说print(lst1[0].get())
。这将返回第一列第一个框中的内容print(lst2[0].get())
将返回第二列第一个框中的内容,这样,您可以得到矩阵类型排列的模式,并且可以轻松地获得如下值
虽然我建议删除条目小部件的绑定,因为它们稍后会给您带来问题,但是如果您正在使用tkinter条目小部件创建占位符,请看一看。虽然课程并不完美,但仍能完成任务
还要记住名称
和金额
都是无用的,因为它们都是无
,所以使用它们是没有意义的。你在帖子中再次重复相同的代码,在你解释不同的方法时,把代码去掉,只包括重要的内容。也许可以尝试将其添加到列表中,并在列表中使用索引来调用所需的项目?@ChrisBoss我不会撒谎,看起来你的代码有点问题,但我可以试着帮你。第一个错误是因为变量“name”仅在函数“new_row”的作用域中可用。首先,我建议您将init函数放在类中的第一位,并将要使用的所有变量列在其中,即self.name=entry\u框。插入(0,“收入名称”)
,然后使用app.name
访问它。还有,为什么你在课堂上有返回语句?我补充了一个答案,让我知道它的工作非常感谢@CoolCloud,几个月的工作,结束在望,谢谢。
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:/Users/Meyerc2/.PyCharmEdu2019.3/config/scratches/scratch_5.py", line 82, in <lambda>
Submitbtn = tk.Button(Window4, text="Submit", command=lambda: budget(name, amount, rate))
NameError: name 'name' is not defined
import tkinter as tk
Window4 = tk.Tk()
Window4.title("Budget Program")
Window4.geometry("700x400")
windowWidth = Window4.winfo_reqwidth()
windowHeight = Window4.winfo_reqheight()
positionRight = int(Window4.winfo_screenwidth() / 2 - windowWidth / 2)
positionDown = int(Window4.winfo_screenheight() / 2 - windowHeight / 2)
Window4.geometry("+{}+{}".format(positionRight, positionDown))
class App(object):
def new_row(self):
new_entry = tk.Entry(Window4, width=15)
name = new_entry.insert(0, "Name of income")
new_entry2 = tk.Entry(Window4, width=8)
amount = new_entry2.insert(0, "Amount")
rate = OptionList = [
"Hourly",
"Daily",
"Weekly",
"Monthly"
]
variable = tk.StringVar(Window4)
variable.set(OptionList[0])
opt = tk.OptionMenu(Window4, variable, *OptionList)
opt.config(width=5, font=('Helvetica', 6))
blank = tk.Label(Window4, text=" ")
# Put widgets in grid
self.num_rows += 1
self.num_rows2 += 1
self.num_rows3 += 1
self.num_rows4 += 1
opt.grid(column=3, row=self.num_rows3)
opt.bind("<Button-1>")
new_entry.grid(column=0, row=self.num_rows)
def some_callback(event):
new_entry.delete(0, "end")
return None
new_entry.bind("<Button-1>", some_callback)
new_entry2.grid(column=1, row=self.num_rows2)
def some_callback(event):
new_entry2.delete(0, "end")
return None
new_entry2.bind("<Button-1>", some_callback)
blank.grid(column=6, row=self.num_rows4, padx=160)
return new_entry, new_entry2, variable
def __init__(self):
self.num_rows = 1
self.num_rows2 = 1
self.num_rows3 = 1
self.num_rows4 = 1
createRow_button = tk.Button(
Window4, text='Add income row', command=self.new_row)
assert isinstance(createRow_button, object)
createRow_button.place(x=240, y=0)
app = App()
def budget(new_entry, new_entry2, variable):
nameincome = new_entry.get()
print(nameincome)
amountincome = new_entry2.get()
print(amountincome)
rateincome = variable.get()
print(rateincome)
Submitlabel = tk.Label(Window4, text="When you are done click below to work out your budget.", )
Submitlabel.place(x=240, y=40)
Submitbtn = tk.Button(Window4, text="Submit", command=lambda: budget(new_entry, new_entry2, variable))
Submitbtn.place(x=365, y=90)
Window4.mainloop()
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:/Users/Meyerc2/.PyCharmEdu2019.3/config/scratches/scratch_5.py", line 87, in <lambda>
Submitbtn = tk.Button(Window4, text="Submit", command=lambda: budget(new_entry, new_entry2, variable))
NameError: name 'new_entry' is not defined
import tkinter as tk
Window4 = tk.Tk()
Window4.title("Budget Program")
Window4.geometry("700x400")
windowWidth = Window4.winfo_reqwidth()
windowHeight = Window4.winfo_reqheight()
positionRight = int(Window4.winfo_screenwidth() / 2 - windowWidth / 2)
positionDown = int(Window4.winfo_screenheight() / 2 - windowHeight / 2)
Window4.geometry("+{}+{}".format(positionRight, positionDown))
class App(object):
def new_row(self):
v = tk.StringVar()
new_entry = tk.Entry(Window4, width=15, textvariable=v)
name = new_entry.insert(0, "Name of income")
mylist = []
new_entry2 = tk.Entry(Window4, width=8)
amount = new_entry2.insert(0, "Amount")
rate = OptionList = [
"Hourly",
"Daily",
"Weekly",
"Monthly"
]
variable = tk.StringVar(Window4)
variable.set(OptionList[0])
opt = tk.OptionMenu(Window4, variable, *OptionList)
opt.config(width=5, font=('Helvetica', 6))
blank = tk.Label(Window4, text=" ")
# Put widgets in grid
self.num_rows += 1
self.num_rows2 += 1
self.num_rows3 += 1
self.num_rows4 += 1
opt.grid(column=3, row=self.num_rows3)
opt.bind("<Button-1>")
new_entry.grid(column=0, row=self.num_rows)
def some_callback(event):
new_entry.delete(0, "end")
return None
new_entry.bind("<Button-1>", some_callback)
new_entry2.grid(column=1, row=self.num_rows2)
def some_callback(event):
new_entry2.delete(0, "end")
return None
new_entry2.bind("<Button-1>", some_callback)
blank.grid(column=6, row=self.num_rows4, padx=160)
mylist.append(name)
mylist.append(amount)
mylist.append(rate)
return new_entry, new_entry2, rate
def __init__(self):
self.num_rows = 1
self.num_rows2 = 1
self.num_rows3 = 1
self.num_rows4 = 1
createRow_button = tk.Button(
Window4, text='Add income row', command=self.new_row)
createRow_button.place(x=240, y=0)
app = App()
new_entry2 = ''
rate = ''
def income():
nameincome = App.new_row().find(App.new_row())
amountincome = App.new_entry2.get()
rateincome = App.rate
print(nameincome, amountincome, rateincome)
btn = tk.Button(Window4, text="go", command=lambda: income())
btn.place(x=270, y=0)
Submitlabel = tk.Label(Window4, text="When you are done click below to work out your budget.", )
Submitlabel.place(x=240, y=40)
Window4.mainloop()
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:/Users/Meyerc2/.PyCharmEdu2019.3/config/scratches/scratch_5.py", line 92, in <lambda>
btn = tk.Button(Window4, text="go", command=lambda: income())
File "C:/Users/Meyerc2/.PyCharmEdu2019.3/config/scratches/scratch_5.py", line 85, in income
nameincome = App.new_row().find(App.new_row())
TypeError: new_row() missing 1 required positional argument: 'self'
import tkinter as tk
Window4 = tk.Tk()
Window4.title("Budget Program")
Window4.geometry("700x400")
windowWidth = Window4.winfo_reqwidth()
windowHeight = Window4.winfo_reqheight()
positionRight = int(Window4.winfo_screenwidth() / 2 - windowWidth / 2)
positionDown = int(Window4.winfo_screenheight() / 2 - windowHeight / 2)
Window4.geometry("+{}+{}".format(positionRight, positionDown))
class App(object):
def new_row(self):
v = tk.StringVar()
new_entry = tk.Entry(Window4, width=15, textvariable=v)
name = new_entry.insert(0, "Name of income")
mylist = []
new_entry2 = tk.Entry(Window4, width=8)
amount = new_entry2.insert(0, "Amount")
rate = OptionList = [
"Hourly",
"Daily",
"Weekly",
"Monthly"
]
variable = tk.StringVar(Window4)
variable.set(OptionList[0])
opt = tk.OptionMenu(Window4, variable, *OptionList)
opt.config(width=5, font=('Helvetica', 6))
blank = tk.Label(Window4, text=" ")
# Put widgets in grid
self.num_rows += 1
self.num_rows2 += 1
self.num_rows3 += 1
self.num_rows4 += 1
opt.grid(column=3, row=self.num_rows3)
opt.bind("<Button-1>")
new_entry.grid(column=0, row=self.num_rows)
def some_callback(event):
new_entry.delete(0, "end")
return None
new_entry.bind("<Button-1>", some_callback)
new_entry2.grid(column=1, row=self.num_rows2)
def some_callback(event):
new_entry2.delete(0, "end")
return None
new_entry2.bind("<Button-1>", some_callback)
blank.grid(column=6, row=self.num_rows4, padx=160)
mylist.append(name)
mylist.append(amount)
mylist.append(rate)
return new_entry, new_entry2, rate
def __init__(self):
self.num_rows = 1
self.num_rows2 = 1
self.num_rows3 = 1
self.num_rows4 = 1
createRow_button = tk.Button(
Window4, text='Add income row', command=self.new_row)
createRow_button.place(x=240, y=0)
app = App()
new_entry2 = ''
rate = ''
def income():
nameincome = App.new_row().find(App.new_row())
amountincome = App.new_entry2.get()
rateincome = App.rate
print(nameincome, amountincome, rateincome)
btn = tk.Button(Window4, text="go", command=lambda: income())
btn.place(x=270, y=0)
Submitlabel = tk.Label(Window4, text="When you are done click below to work out your budget.", )
Submitlabel.place(x=240, y=40)
Window4.mainloop()
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:/Users/Meyerc2/.PyCharmEdu2019.3/config/scratches/scratch_5.py", line 89, in <lambda>
btn = tk.Button(Window4, text="go", command=lambda: income())
File "C:/Users/Meyerc2/.PyCharmEdu2019.3/config/scratches/scratch_5.py", line 83, in income
nameincome = App.new_row().find(App.new_row())
TypeError: new_row() missing 1 required positional argument: 'self'
import tkinter as tk
Window4 = tk.Tk()
Window4.title("Budget Program")
Window4.geometry("700x400")
windowWidth = Window4.winfo_reqwidth()
windowHeight = Window4.winfo_reqheight()
positionRight = int(Window4.winfo_screenwidth() / 2 - windowWidth / 2)
positionDown = int(Window4.winfo_screenheight() / 2 - windowHeight / 2)
Window4.geometry("+{}+{}".format(positionRight, positionDown))
class App(object):
def new_row(self):
Unique_Val = 0
Unique_Val_A = 0
v = tk.StringVar()
entry_Box = "new_entry{}".format(Unique_Val)
entry_Box = tk.Entry(Window4, width=15, textvariable=v)
name = entry_Box.insert(0, "Name of income")
Unique_Val += 1
entry_Box2 = "new_entry2{}".format(Unique_Val_A)
entry_Box2 = tk.Entry(Window4, width=8)
amount = entry_Box2.insert(0, "Amount")
Unique_Val_A += 1
rate = OptionList = [
"Hourly",
"Daily",
"Weekly",
"Monthly"
]
variable = tk.StringVar(Window4)
variable.set(OptionList[0])
opt = tk.OptionMenu(Window4, variable, *OptionList)
opt.config(width=5, font=('Helvetica', 6))
blank = tk.Label(Window4, text=" ")
# Put widgets in grid
self.num_rows += 1
self.num_rows2 += 1
self.num_rows3 += 1
self.num_rows4 += 1
opt.grid(column=3, row=self.num_rows3)
opt.bind("<Button-1>")
entry_Box.grid(column=0, row=self.num_rows)
def some_callback(event):
entry_Box.delete(0, "end")
return None
entry_Box.bind("<Button-1>", some_callback)
entry_Box2.grid(column=1, row=self.num_rows2)
def some_callback(event):
entry_Box2.delete(0, "end")
return None
entry_Box2.bind("<Button-1>", some_callback)
blank.grid(column=6, row=self.num_rows4, padx=160)
return entry_Box, entry_Box2, rate
def save(self):
name1 = self.entry_Box.get()
amount1 = self.entry_Box2.get()
mylist = []
mylist.append(name1)
mylist.append(amount1)
print(mylist)
def __init__(self):
self.num_rows = 1
self.num_rows2 = 1
self.num_rows3 = 1
self.num_rows4 = 1
createRow_button = tk.Button(
Window4, text='Add income row', command=self.new_row)
createRow_button.place(x=240, y=0)
savebtn = tk.Button(Window4, text="save", command=self.save)
savebtn.place(x=240, y=50)
app = App()
Window4.mainloop()
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:/Users/Meyerc2/.PyCharmEdu2019.3/config/scratches/scratch_1.py", line 69, in save
name1 = self.entry_Box.get()
AttributeError: 'App' object has no attribute 'entry_Box'
def __init__(self):
.....#same bunch of code
self.lst1 = []
self.lst2 = []
def new_row():
....#same bunch of codes
self.lst1.append(entry_Box)
self.lst2.append(entry_Box2)