2
import tkinter as tk

root = tk.Tk()
root.title('Book')

class phonebook:
    def __init__(self,first_name, last_name, street, postcode, city, number):
        self.first_name = first_name
        self.last_name = last_name
        self.street = street
        self.postcode = postcode
        self.city = city
        self.number = number     

    def create(self):
        creation = tk.Tk()

        tk.Label(creation, text = 'First Name').grid(row = 0, column = 0)
        tk.Label(creation, text = 'Last Name').grid(row = 1, column = 0)
        tk.Label(creation, text = 'Stadt').grid(row = 2, column = 0)
        tk.Label(creation, text = 'Postleitzahl').grid(row = 3, column = 0)
        tk.Label(creation, text = 'Straße').grid(row = 4, column = 0)
        tk.Label(creation, text = 'Telefonnummer').grid(row = 5, column = 0)

        self.first_name = tk.Entry(creation)
        self.last_name = tk.Entry(creation)
        self.street = tk.Entry(creation)
        self.postcode = tk.Entry(creation)
        self.city = tk.Entry(creation)
        self.number = tk.Entry(creation)   


menubar = tk.Menu(root)
root.config(menu = menubar)
menubar.add_command(label = 'Anlegen', command = self.create)
menubar.add_command(label = 'Bearbeiten')
menubar.add_command(label = 'Löschen')
menubar.add_command(label = 'Sortieren')
menubar.add_command(label = 'Suche')
menubar.add_command(label = 'Hilfe')

root.mainloop() 

When I try the code above I get this error:

Traceback (most recent call last):
  File "C:/Users/Lenovo M93p/Dropbox/Uni Dropbox/EPR/EPR_Blatt05_Jonas_Emrich_Karl_Wilhelm_Viebach/geathb.py", line 45, in <module>
    menubar.add_command(label = 'Anlegen', command = self.create)
NameError: name 'self' is not defined

When I change:

menubar.add_command(label = 'Anlegen', command = self.create)

to:

menubar.add_command(label = 'Anlegen', command = phonebook.create)

I get this error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Lenovo M93p\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1550, in __call__
    return self.func(*args)
TypeError: create() missing 1 required positional argument: 'self'

I was trying to change something in the create function, changing the command line, but I get an error every time.

2
  • 1
    Quickly: create a phonebook object and call the create method on it instead of self.create Commented Dec 16, 2016 at 12:06
  • @KuKeC Your edit messed up the indentation! Commented Dec 16, 2016 at 12:26

1 Answer 1

3

Less quickly:

In your original code, you are calling the create method on self, which is not declared in the scope.

You want to call the create method of the phonebook class, so you must instantiate the latter first. Basically, something like this:

p = phonebook(John, Doe, Evergreen Terrace, 12, Washington, 000)
p.create()

will work as intended. In your situation, you are calling that method through another one, which becomes:

p = phonebook(...)
menubar.add_command(label = 'Anlegen', command = p.create)

Side note

In Python, always name your classes starting with a capital letter, like Phonebook. This will avoid you confusions between classes and instances.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.