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.
phonebookobject and call thecreatemethod on it instead ofself.create