0

I managed to build a little GUI, where I can switch between frames related to the following question: Switch between frames in tkinter

I wanted to have a status bar on the Bottom of my GUI and it should stay on every frame! The status bar shows info about buttons when hovering over it!

Not hovered:

enter image description here

Hovered:

enter image description here

As you can see it works, BUT I can't manage to make the status bar update its values when another frame is raised, because I could only manage to create the status bar in one frame class...

class SampleApp(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        self.title_font = font.Font(family='Helvetica', size=18, 
         weight="bold", slant="italic")
         container = Frame(self)
         container.pack(side="top", fill="both", expand=True)
         container.grid_rowconfigure(0, weight=1)
         container.grid_columnconfigure(0, weight=1)

         self.frames = {}
         for F in (start_frame, cr_frame, db_frame):
             page_name = F.__name__
             frame = F(parent=container, controller=self)
             self.frames[page_name] = frame
             frame.pack()
         self.show_frame("start_frame")

     def show_frame(self, page_name):
         '''Show a frame for the given page name'''
         for frame in self.frames.values():
            #frame.grid_remove()
            frame.pack_forget()
        frame = self.frames[page_name]
        frame.pack()

        if page_name == "start_frame":
            frame.winfo_toplevel().geometry("545x200")
        if page_name == "cr_frame":
            frame.winfo_toplevel().geometry("600x200")
        if page_name == "db_frame":
            frame.winfo_toplevel().geometry("700x630")

class start_frame(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller

    self.btn_cr = Button(self, text="Copyright Analyse", command=lambda: self.controller.show_frame("cr_frame"), width=40)        
    self.btn_cr.pack(side=LEFT, padx=15, pady=1, ipady=40)
    self.btn_db = Button(self, text="Copyright Datenbank", command=lambda: self.controller.show_frame("db_frame"), width=40)
    self.btn_db.pack(side=LEFT, pady=1, ipady=40)


    ###################Here is the statusbar defined + bindings and so on##########
    self.lbl_status = Label(self.controller, text="...", border=1, relief=SUNKEN, anchor=W)
    self.lbl_status.pack(side=BOTTOM, fill=X, anchor=W)
    self.btn_cr.bind("<Enter>", lambda event: self.lbl_status.configure(text="Open copyright analysis window..."))
    self.btn_cr.bind("<Leave>", self.leave_bindings)
    self.btn_db.bind("<Enter>", lambda event: self.lbl_status.configure(text="Open copyright database..."))
    self.btn_db.bind("<Leave>", self.leave_bindings)


class cr_frame(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller

Now, if I switch to page 2 (cr_frame), the status bar is still there because I attached it to the top-level controller (of all frames) but I can't edit it through the cr_frame class...

I don't know how to do it.

maciejwww
  • 1,067
  • 1
  • 13
  • 26
Dany
  • 9
  • 4

1 Answers1

0

First, move the statusbar to the main app since it's part of the app and not part of a page:

class SampleApp(Tk):
    def __init__(self, *args, **kwargs):
        ...
        container = Frame(self)
        self.lbl_status = Label(self, text="", border=1, relief=SUNKEN, anchor=W)

        container.pack(side="top", fill="both", expand=True)
        self.lbl_status.pack(side="bottom", fill="x")

Next, add a method to the app for setting the status:

class SampleApp(Tk):
    ...
    def set_status(self, string):
        self.lbl_status.configure(text=string)

Finally, call that method whenever you need to change the status:

class cr_frame(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller
    ...
    def something(self):
        ...
        self.controller.set_status("Hello, world")
        ...
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks a lot! Was easier than I excpected but I had to add controller to the call from the other frames to get this to work :) Thanks a lot though! – Dany May 21 '18 at 09:20