Python Forum
[Tkinter] Toplevel window and global widgets?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Toplevel window and global widgets?
#1
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.
Reply
#2
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()
Reply
#3
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?
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 493 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  pass a variable between tkinter and toplevel windows janeik 10 2,329 Jan-24-2024, 06:44 AM
Last Post: Liliana
  [Tkinter] Open tkinter colorchooser at toplevel (so I can select/focus on either window) tabreturn 4 1,902 Jul-06-2022, 01:03 PM
Last Post: deanhystad
  [Tkinter] Toplevel window menator01 5 3,039 Apr-18-2022, 06:01 PM
Last Post: menator01
  [Tkinter] Not able to get image as background in a Toplevel window finndude 4 3,914 Jan-07-2022, 10:10 PM
Last Post: finndude
  [Tkinter] Images in Toplevel() finndude 4 4,294 Mar-09-2021, 09:39 AM
Last Post: finndude
  Create image on a Toplevel using tkinter ViktorWong 3 7,822 Jun-13-2020, 03:21 PM
Last Post: deanhystad
  [Tkinter] Connect Toplevel Radiobuttons to root Label/Entry widgets iconit 2 2,450 Apr-28-2020, 06:50 AM
Last Post: iconit
  Using Tkinter widgets on child window chewy1418 8 7,223 Feb-27-2020, 10:34 PM
Last Post: Marbelous
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,467 Jan-23-2020, 09:00 PM
Last Post: HBH

Forum Jump:

User Panel Messages

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