用户输入后,将条目的值保存在重复功能中

如何解决用户输入后,将条目的值保存在重复功能中

嗨,我的名字叫克里斯,我的代码遍地都是,这 也是我第一次问一个问题。所以我的问题是我有 一个按钮,该按钮每次按下都会运行一个功能, 函数在一个花药旁边创建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。不是完美的课程,但仍然可以完成工作。

请记住,nameamount就像None一样没有用,因此使用它们毫无意义。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?