如何解决用户输入后,将条目的值保存在重复功能中
嗨,我的名字叫克里斯,我的代码遍地都是,这 也是我第一次问一个问题。所以我的问题是我有 一个按钮,该按钮每次按下都会运行一个功能, 函数在一个花药旁边创建3个条目,其中用户是 希望在每个输入框中输入适当的信息,我的 问题是我添加了一个按钮,当按下该按钮时,它应该拉 每个现有条目行中的值并将其保存,我想在以后保存 用它来做方程式。我的问题是,由于我重复 条目,它们不是每次都唯一,而是一个克隆,所以我不能 似乎告诉程序查找每个条目,因为它们是 彼此,或者至少就是我的想法。
我尝试了许多不同的方法来保存值,所以这里是每个 我尝试的方法:
这是我代码的一部分,没有代码,我尝试保存 值:
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 name
return amount
return 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()
def budget(name,amount,rate):
nameincome = name.get()
print(nameincome)
amountincome = amount.get()
print(amountincome)
rateincome = rate.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(name,rate))
Submitbtn.place(x=365,y=90)
Window4.mainloop()
我首先尝试简单地使用.get()函数:
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,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,"Amount")
rate = OptionList = [
"Hourly","Monthly"
]
variable = tk.StringVar(Window4)
variable.set(OptionList[0])
opt = tk.OptionMenu(Window4,row=self.num_rows)
def some_callback(event):
new_entry.delete(0,row=self.num_rows2)
def some_callback(event):
new_entry2.delete(0,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,command=self.new_row)
assert isinstance(createRow_button,object)
createRow_button.place(x=240,y=0)
app = App()
def budget(new_entry,variable):
nameincome = new_entry.get()
print(nameincome)
amountincome = new_entry2.get()
print(amountincome)
rateincome = variable.get()
print(rateincome)
Submitlabel = tk.Label(Window4,command=lambda: budget(new_entry,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 87,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,"end")
return None
new_entry.bind("<Button-1>","end")
return None
new_entry2.bind("<Button-1>",padx=160)
mylist.append(name)
mylist.append(amount)
mylist.append(rate)
return new_entry,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,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,y=40)
Window4.mainloop()
接下来,我尝试将其保存到列表中
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\tkinter\__init__.py",line 92,in <lambda>
btn = tk.Button(Window4,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,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,rateincome)
btn = tk.Button(Window4,y=0)
Submitlabel = tk.Label(Window4,y=40)
Window4.mainloop()
接下来,我尝试使用App()函数从类中获取值,以便可以在函数内使用它:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python38\lib\tkinter\__init__.py",line 89,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,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",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>",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,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",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'
但是我得到了错误:
say
如果您需要其他任何信息,我们很乐意为您提供帮助, 如果需要GitHub,我也可以在必要时提供我的完整代码, 我正在使用pycharm。
解决方法
我添加这个只是为了让您对做什么或做什么有所了解,这仅仅是继续进行的方式之一。
基本上,只需在__init__
调用中创建两个空列表,然后将这些条目小部件附加到先前创建的那些列表中,例如:
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输入小部件创建占位符,请查看here。不是完美的课程,但仍然可以完成工作。
请记住,name
和amount
就像None
一样没有用,因此使用它们毫无意义。