Python Forum
TypeError: exitfunc() missing 1 required positional argument: 'self'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: exitfunc() missing 1 required positional argument: 'self'
#1
HI All,

I am new to the python, i am trying to call a method defined in the same class but getting an error as
" return self.func(*args)
TypeError: exitfunc() missing 1 required positional argument: 'self'"

The code is below : Trying to write a simple Menu window using tkinter.

from tkinter import *
import tkinter.messagebox
from tkinter.filedialog import askopenfilename

class menudiaplay:

    def __init__(self):
        self.exit = exit

    def function1(self):
     print("Menu is clicked")


    def openfile(self):
      name = askopenfilename()
      print(name)

    def function3(self):
       print("Undo is done")

    def exitfunc(self):
        self.exit = tkinter.messagebox.askquestion("Question", "Really want to exit")
        if self.exit == 'yes':
            self.root.destroy()
        else:
            print("Hello welcome back")


    root = Tk()
    mymenu = Menu(root)
    root.config(menu=mymenu)


    filesubmenu = Menu(mymenu)

    mymenu.add_cascade(label="File", menu=filesubmenu)

    filesubmenu.add_cascade(label="New Project", command=function1)
    filesubmenu.add_cascade(label="New", command=function1)
    filesubmenu.add_cascade(label="open File", command=askopenfilename)
    filesubmenu.add_cascade(label="Power save Mode", command=function1)
    filesubmenu.add_cascade(label="Exit", command=exitfunc)

    editsubmenu = Menu(mymenu)

    mymenu.add_cascade(label="Edit", menu=editsubmenu)

    editsubmenu.add_cascade(label="Undo", command=function3)

    toolbar = Frame(root, bg="light pink")
    insertbutton = Button(toolbar, text="Insert Files", command=function1)
    insertbutton.pack(side=LEFT, padx=2, pady=3)

    printbuttopn = Button(toolbar, text="Print", command=function1)
    printbuttopn.pack(side=LEFT, padx=2, pady=3)

    toolbar.pack(side=TOP, fill=X)

    status = Label(root, text="This is the status", bd=1, relief=SUNKEN, anchor=W)
    status.pack(side=BOTTOM, fill=X)

    root.mainloop()


b = menudiaplay()
Please help me.Thanks in advance.
Reply
#2
change:
filesubmenu.add_cascade(label="Exit", command=exitfunc)
to:
filesubmenu.add_cascade(label="Exit", command=self.exitfunc)
Reply
#3
Getting error as,

filesubmenu.add_cascade(label="Exit", command=self.exitfunc)
NameError: name 'self' is not defined
Reply
#4
there are many things wrong with this script.
You are not using class properly.

much of what is here is outside of the class.
Use:
from tkinter import *
import tkinter.messagebox
from tkinter.filedialog import askopenfilename


class menudiaplay:
    def __init__(self, parent):
        self.parent = parent
        self.exit = exit
        mymenu = Menu(self.parent)
        self.parent.config(menu=mymenu)

        filesubmenu = Menu(mymenu)

        mymenu.add_cascade(label="File", menu=filesubmenu)

        filesubmenu.add_cascade(label="New Project", command=self.function1)
        filesubmenu.add_cascade(label="New", command=self.function1)
        filesubmenu.add_cascade(label="open File", command=askopenfilename)
        filesubmenu.add_cascade(label="Power save Mode", command=self.function1)
        filesubmenu.add_cascade(label="Exit", command=self.exitfunc)

        editsubmenu = Menu(mymenu)

        mymenu.add_cascade(label="Edit", menu=editsubmenu)

        editsubmenu.add_cascade(label="Undo", command=self.function3)

        toolbar = Frame(self.parent, bg="light pink")
        insertbutton = Button(toolbar, text="Insert Files", command=self.function1)
        insertbutton.pack(side=LEFT, padx=2, pady=3)

        printbuttopn = Button(toolbar, text="Print", command=self.function1)
        printbuttopn.pack(side=LEFT, padx=2, pady=3)

        toolbar.pack(side=TOP, fill=X)

        status = Label(self.parent, text="This is the status", bd=1, relief=SUNKEN, anchor=W)
        status.pack(side=BOTTOM, fill=X)

    def function1(self):
        print("Menu is clicked")

    def openfile(self):
        name = askopenfilename()
        print(name)

    def function3(self):
        print("Undo is done")

    def exitfunc(self):
        self.exit = tkinter.messagebox.askquestion("Question", "Really want to exit")
        if self.exit == 'yes':
            self.parent.destroy()
        else:
            print("Hello welcome back")

def main():
    root = Tk()
    menudiaplay(root)
    root.mainloop()

if __name__ == '__main__':
    main()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: a bytes-like object is required ZeroX 13 3,841 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 1,858 Oct-17-2022, 06:29 PM
Last Post: paulo79
  TypeError: a bytes-like object is required, not 'str' - Help Please. IanJ 3 4,674 Aug-29-2022, 05:53 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,767 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Error: _vhstack_dispatcher() takes 1 positional argument but 9 were given alexfrol86 3 5,721 May-09-2022, 12:49 PM
Last Post: deanhystad
  What is positional argument self? Frankduc 22 5,493 Mar-06-2022, 01:18 AM
Last Post: Frankduc
  TypeError: missing a required argument: 'y' gible 0 2,847 Dec-15-2021, 02:21 AM
Last Post: gible
  positional argument: 'self' mcmxl22 8 3,195 Dec-13-2021, 10:11 PM
Last Post: deanhystad
  TypeError: missing 3 required positional arguments: wardancer84 9 10,666 Aug-19-2021, 04:27 PM
Last Post: deanhystad
  TypeError: run_oracle_job() missing 1 required positional argument: 'connection_strin python_student 1 1,939 Aug-06-2021, 08:05 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020