Python Forum
Tkinter object scope
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter object scope
#7
This is easy to figure out if you let Python help.

First you look at the error message:
Error:
book = Book(self.entBookTitle.get(), self.entBookISBN.get(), self.entBookAuthor.get(), self.entBookYear.get()) ^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'get'
The next thing I would do is put a breakpoint on line 32 and run the program in the debugger, or add a print command before line 32 to display the value of self.entBookTitle.
    def SaveBook(self):
        print(self.entBookTitle)
        book = Book(self.entBookTitle.get(), self.entBookISBN.get(), self.entBookAuthor.get(), self.entBookYear.get())
This would verify that self.entBookTitle is indeed None.

Next search for every line that includes entBookTitle and focus on lines that assign a value to entBookTitle. In your posted code this only happens on line 17.
        self.entBookTitle = ttk.Entry(frameRight, font=('Helvetica', 12), width=20).pack(padx=10, pady=10)
Using the debugger I would set a breakpoint on line 18, or add a print statement right after line 17 to see the value of self.entBookTitle right after the assignment. The value would indeed be Null. Now you have isolated the problem to 1 line which contains an assignment and two function calls. These are the two function calls:
ttk.Entry(frameRight, font=('Helvetica', 12), width=20)
.pack(padx=10, pady=10)
One of these returns None. I would rewrite like this:
        self.entBookTitle
temp = ttk.Entry(frameRight, font=('Helvetica', 12), width=20)
print("entry", temp)
temp.pack(padx=10, pady=10)
print("pack", temp)
self.entBookTitle = temp
Run the code and you would see that "ttk.Entry(frameRight, font=('Helvetica', 12), width=20)" returns a ttkEntry object and pack() returns None. Armed with that information you would quickly realize that you need to break up creating and packing the entry widget.
self.entBookTitle = ttk.Entry(frameRight, font=('Helvetica', 12), width=20)
self.entBooktitle.pack(padx=10, pady=10)
I'm confident that you would have solved this problem quite quickly if you did a little poking around instead of immediately throwing up your hands in frustration. Give yourself some credit.
Reply


Messages In This Thread
Tkinter object scope - by riversr54 - Feb-16-2023, 07:40 PM
RE: Tkinter object scope - by riversr54 - Feb-16-2023, 07:41 PM
RE: Tkinter object scope - by Gribouillis - Feb-16-2023, 07:55 PM
RE: Tkinter object scope - by riversr54 - Feb-16-2023, 08:14 PM
RE: Tkinter object scope - by Gribouillis - Feb-16-2023, 08:54 PM
RE: Tkinter object scope - by riversr54 - Feb-16-2023, 09:19 PM
RE: Tkinter object scope - by deanhystad - Feb-17-2023, 05:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter AttributeError: 'GUI' object has no attribute pfdjhfuys 3 1,643 May-18-2023, 03:30 PM
Last Post: pfdjhfuys
  Key Binding scope angus1964 1 1,247 Jun-30-2022, 08:17 PM
Last Post: deanhystad
  tkinter moving an class object with keybinds gr3yali3n 5 3,334 Feb-10-2021, 09:13 PM
Last Post: deanhystad
  Scope of variable when using two classes AyJay 2 2,208 Jan-23-2020, 10:09 AM
Last Post: AyJay

Forum Jump:

User Panel Messages

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