Feb-16-2023, 07:40 PM
New to Python but really new to Tkinter...
Working on a simple app that has multiple child pages and one 'home' page that dispatches to the 'child' pages. Each one of the child pages is defined as a class inherited from Toplevel. In that class there is a constructor (__init__) that does some setup and defines the controls on the page (Button and Entry). One of the Buttons calls a second function in the class named SaveData. The idea is that this function will collect data from the Entry objects and save it in a custom class(Book). So far, so good... my problem arises when I try to access the data in the Entry object inside the SaveData function. The Entry object was declared in the __init__ function. I get an error indicating that the Entry object is None. This makes perfect sense to me given that the Entry object was defined in a different function. The question is how do I make it Global so that it is available in all the functions of the class or is there some other way? I've done some experimenting with making the Entry object global in __init__ but with no success.
Thanks for any advice/suggestions. Code Below
rivers
Working on a simple app that has multiple child pages and one 'home' page that dispatches to the 'child' pages. Each one of the child pages is defined as a class inherited from Toplevel. In that class there is a constructor (__init__) that does some setup and defines the controls on the page (Button and Entry). One of the Buttons calls a second function in the class named SaveData. The idea is that this function will collect data from the Entry objects and save it in a custom class(Book). So far, so good... my problem arises when I try to access the data in the Entry object inside the SaveData function. The Entry object was declared in the __init__ function. I get an error indicating that the Entry object is None. This makes perfect sense to me given that the Entry object was defined in a different function. The question is how do I make it Global so that it is available in all the functions of the class or is there some other way? I've done some experimenting with making the Entry object global in __init__ but with no success.
Thanks for any advice/suggestions. Code Below
rivers
from tkinter import * from tkinter import ttk from Book import * class EnterBookWindow(Toplevel): [color=#2980B9]def __init__(self):[/color] Toplevel.__init__(self) self.title("Enter a Book in xx Data Structure") self.geometry('400x400') frameLeft = Frame(self, width=40, padx=20, pady=20) frameLeft.pack(side=LEFT) frameRight = Frame(self, width=40, padx=20, pady=20) frameRight.pack(side=RIGHT) lblTitle = ttk.Label(frameRight, text="Title:").pack(padx=10, pady=0) [color=#C0392B]global entBookTitle[/color] entBookTitle = ttk.Entry(frameRight, font=('Helvetica', 12), width=20).pack(padx=10, pady=10) lblISBN = ttk.Label(frameRight, text="ISBN:").pack(padx=10, pady=0) entBookISBN = ttk.Entry(frameRight, font=('Helvetica', 12), width=20).pack(padx=10, pady=10) lblAuthor = ttk.Label(frameRight, text="Author:").pack(padx=10, pady=0) entBookAuthor = ttk.Entry(frameRight, font=('Helvetica', 12), width=20).pack(padx=10, pady=10) lblYear = ttk.Label(frameRight, text="Year:").pack(padx=10, pady=0) entBookYear = ttk.Entry(frameRight, font=('Helvetica', 12), width=20).pack(padx=10, pady=10) btnSave = ttk.Button(frameRight, text="Save Data", command=self.SaveBook).pack(padx=20, pady=10) def SaveBook(self): book = Book(entBookTitle.get(), entBookISBN.get(), entBookAuthor.get(), entBookYear.get()) #!!!Error occurs here when trying to get data from entBookTitle