Python Forum

Full Version: Toplevel window and global widgets?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am using a Toplevel window for users to enter a record number (entrcrd) which is to be opened from an SQLite database.

At the initiation of the button btnopen and its command procedure I want the Toplevel window to close automatically.

The only means I have yet found to do this is to make the widgets entrcrd (Enter record ) and btnexit ( Exit) as globals and then invoke the command of the btnexit to destroy the Toplevel window filewin.

My question is this, is this approach, sloppy coding and could the objectives be achieved in a more refined manner?

Any suggestions comments or observations most welcome.

def record_open_which():
    global entrcrd
    global btnexit

    filewin=Toplevel(window)
    filewin.resizable(0, 0)
    filewin.geometry('300x100+800+40')
    filewin.title('Open a record')
    filewin.tk.call('wm', 'iconphoto', filewin._w, PhotoImage(file='resources/part.png'))

    entrcrd=Entry(filewin, relief='groove', width=12, font=('Tahoma', '7'))
    entrcrd.place(x=205, y=18)

    btnopen=Button(filewin)
    btnopen.place(x=205, y=40)
    btnopen.configure(text='Open Record', width=9, font=('Tahoma', '7'))

    btnexit=Button(filewin)
    btnexit.place(x=205, y=60)
    btnexit.configure(text='Exit', width=9, font=('Tahoma', '7'))

    btnopen.configure(command=record_to_open)
    btnexit.configure(command=filewin.destroy)


def record_to_open():
    Rcrd = entrcrd.get()
    record_open(Rcrd)
    btnexit.invoke()

Apologies Admin I've done it again, could you please move this to the general coding help folder.
Use class'es, widgets can be accessed as attributes of the class
import tkinter as tk


class RecordOpenWhichFrame(tk.Toplevel):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.resizable(0, 0)
        self.geometry('300x100+800+40')
        self.title('Open a record')
#         self.tk.call('wm', 'iconphoto',
#                      self._w, tk.PhotoImage(file='resources/part.png'))
     
        self.entrcrd = tk.Entry(
            self, relief='groove', width=12, font=('Tahoma', '7'))
        self.entrcrd.place(x=205, y=18)
     
        btnopen = tk.Button(self)
        btnopen.place(x=205, y=40)
        btnopen.configure(text='Open Record', width=9, font=('Tahoma', '7'))
     
        btnexit = tk.Button(self)
        btnexit.place(x=205, y=60)
        btnexit.configure(text='Exit', width=9, font=('Tahoma', '7'))
     
        btnopen.configure(command=self.record_to_open)
        btnexit.configure(command=self.destroy)
 
    def record_to_open(self):
        rcrd = self.entrcrd.get()
        RecordOpenFrame(rcrd=rcrd)
        self.destroy()
    

class RecordOpenFrame(tk.Toplevel):

    def __init__(self, *args, **kwargs):
        rcrd = kwargs.pop('rcrd')
        super().__init__(*args, **kwargs)
        self.geometry('300x100+800+40')
        self.title(rcrd)
 

if __name__ == '__main__':
    app = tk.Tk()
    main_window = tk.Frame(app)
    RecordOpenWhichFrame(main_window)
    app.mainloop()
Thank you Yoriz for the very prompt reply.

I'll need some time to read over this and understand *args, **kwargs with classes etc.

Without fear of getting this completely wrong, is it correct to say that you have defined two members of TopLevel class?
The *args, **kwargs are just passing the attributes through to the __init__ of tk.Toplevel.
Yes i have turned your code into classes that inherit from tk.Toplevel.