首页 > 解决方案 > How to get pass error : TypeError: addtrans() missing 1 required positional argument: 'self'

问题描述

i created a button function 'addtrans" that will create another pop up window and collect some info from user entered into a entry box. That function is a button command. When i click button nothing comes up except the error.

def addtrans(self): 
    newWin = Toplevel(root) 
        width = 600
        height = 400 
        screenwidth = root.winfo_screenwidth()
        screenheight = root.winfo_screenheight()
        alignstr = '%dx%d+%d+%d' % (width, height,
                                (screenwidth - width) / 2, (screenheight - height) / 2)
        newWin.geometry(alignstr)
        newWin.resizable(width=False, height=False)   
        self.lbl1 = Label(newWin, text="Enter amount in Euros :", fg='red')
        self.lbl1.place(x=150 , y=50)
        self.euros = Entry(newWin, width=20)
        self.euros.place(x=300, y=50)
        self.lbl2 = Label(newWin, text="CATEGORY :", fg='green') 
        self.lbl2.place(x=150 , y=80)
        self.cat_entry = Entry(newWin, width=20)
        self.cat_entry.place(x=299, y=80)
        self.place_entry = Label(newWin, text=" ENTER NAME OF PLACE :")
        self.place_entry.place(x=140, y=120)
        self.txtfld2 = Entry(newWin, width=20)
        self.txtfld2.place(x=299, y=120)
        self.submit_btn=Button(newWin, text="SUBMIT RECORD TO DATABASE", command=submit)
        self.submit_btn.place(x=280, y=160)

标签: pythonuser-interfacetkinter

解决方案


假设您的代码如下:

class Example(object):
    def __init__(self, root, command):
        self.root = root
        self.command = command

    def addtrans(self): 
        root = self.root
        command = self.command
        newWin = Toplevel(root) 
        width = 600
        height = 400 
        screenwidth = root.winfo_screenwidth()
        screenheight = root.winfo_screenheight()
        alignstr = '%dx%d+%d+%d' % (width, height,
                                (screenwidth - width) / 2, (screenheight - height) / 2)
        newWin.geometry(alignstr)
        newWin.resizable(width=False, height=False)   
        self.lbl1 = Label(newWin, text="Enter amount in Euros :", fg='red')
        self.lbl1.place(x=150 , y=50)
        self.euros = Entry(newWin, width=20)
        self.euros.place(x=300, y=50)
        self.lbl2 = Label(newWin, text="CATEGORY :", fg='green') 
        self.lbl2.place(x=150 , y=80)
        self.cat_entry = Entry(newWin, width=20)
        self.cat_entry.place(x=299, y=80)
        self.place_entry = Label(newWin, text=" ENTER NAME OF PLACE :")
        self.place_entry.place(x=140, y=120)
        self.txtfld2 = Entry(newWin, width=20)
        self.txtfld2.place(x=299, y=120)
        self.submit_btn=Button(newWin, text="SUBMIT RECORD TO DATABASE", command=command)
        self.submit_btn.place(x=280, y=160)

您可以像这样实例化和调用:

import tkinter

win = tkinter.Tk()
example = Example(win, lambda: print("submit!"))
example.addtrans()
win.mainloop()

如果您单击“提交记录到数据库”按钮,它将打印“提交!”


推荐阅读