3

I have build a multi-windows application in Python 3.6 following a tutorial that uses tkinter as GUI. The windows are frame objects within a container. Now I try to figure out how to pass information from one window to another. So far I found this solution:

file main.py:

import tkinter as tk
import SecondPage


class TestApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        # Initialize Window
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}

        pages = (StartPage, SecondPage.SecondPage)
        # Load all pages
        for F in pages:
            frame = F(container, self, pages)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(StartPage)


        self.show_frame(StartPage)

    # shows the desired frame
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
        return True

    # passes text to the window StartPage
    def pass_on_text(self, text):
        self.frames[StartPage].get_text(text)

    # passes text to the window SecondPage
    def pass_on_text2(self, text):
        self.frames[SecondPage.SecondPage].get_text(text)


class StartPage(tk.Frame):
    def __init__(self, parent, controller, pages):
        tk.Frame.__init__(self, parent)

        # define GUI elements
        self.label = tk.Label(self, text="test")
        button = tk.Button(self, text="2nd Page", command=lambda: controller.show_frame(pages[1]))
        but_change2nd = tk.Button(self, text="Change Label on SecondPage", command=lambda: send_text("Hello, too!"))

        # place GUI elements
        self.label.grid(row=0, column=0)
        button.grid(row=1, column=0)
        but_change2nd.grid(row=2, column=0)

        # send text to SecondPage
        def send_text(text):
            controller.pass_on_text2(text)

    # get information and change the displayed text
    def get_text(self, text):
        self.label.config(text=text)


app = TestApp()
app.mainloop()

file SecondPage.py:

import tkinter as tk


class SecondPage(tk.Frame):
    def __init__(self, parent, controller, pages):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        # define GUI elements
        self.label = tk.Label(self, text="test2")
        button = tk.Button(self, text="1st Page", command=lambda: controller.show_frame(pages[0]))
        but_change1st = tk.Button(self, text="Change Label on Startpage", command=lambda: send_text("Hello"))

        # place GUI elements
        self.label.grid(row=0, column=0)
        button.grid(row=1, column=0)
        but_change1st.grid(row=2, column=0)

        # send text to StartPage
        def send_text(text):
            controller.pass_on_text(text)

    # get information and change the displayed text
    def get_text(self, text):
        self.label.config(text=text)

This approach is working, but I am wondering, if there is an easier way to have a button on SecondPage that changes the label on StartPage and visa versa.

YoTcA
  • 79
  • 1
  • 4

1 Answers1

4

I have had this problem for days now and seconds after posting the question I suddenly got an idea that seems to work:

I changed the page variable to be an instance variable:

pages = (StartPage, SecondPage.SecondPage)

into

self.pages = (StartPage, SecondPage.SecondPage)

Then i passed the pages to every Frame on initialisation:

frame = F(container, self, self.pages)

and added a pages variable to the __init__ of every frame:

class StartPage(tk.Frame):
    def __init__(self, parent, controller, pages):

now, I changed the send_text fuction into:

def send_text(text):
        controller.frames[pages[1]].label.config(text=text)

The whole main.py file now looks like this:

import tkinter as tk
import SecondPage


class TestApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)

        # Initialize Window
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}

        self.pages = (StartPage, SecondPage.SecondPage)
        #self.pages = pages
        # Load all pages
        for F in self.pages:
            frame = F(container, self, self.pages)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.show_frame(StartPage)


        self.show_frame(StartPage)

    # shows the desired frame
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
        return True

    # passes text to the window StartPage
    def pass_on_text(self, text):
        self.frames[self.pages[0]].get_text(text)

    # passes text to the window SecondPage
    def pass_on_text2(self, text):
        self.frames[self.pages[1]].get_text(text)


class StartPage(tk.Frame):
    def __init__(self, parent, controller, pages):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.pages = pages
        # define GUI elements
        self.label = tk.Label(self, text="test")
        button = tk.Button(self, text="2nd Page", command=lambda: controller.show_frame(pages[1]))
        but_change2nd = tk.Button(self, text="Change Label on SecondPage", command=lambda: send_text("Hello, too!"))

        # place GUI elements
        self.label.grid(row=0, column=0)
        button.grid(row=1, column=0)
        but_change2nd.grid(row=2, column=0)

        # send text to SecondPage
        def send_text(text):
            controller.frames[pages[1]].label.config(text=text)


    # get information and change the displayed text
    def get_text(self, text):
        self.label.config(text=text)


app = TestApp()
app.mainloop()
YoTcA
  • 79
  • 1
  • 4
  • The whole point of the controller in the code you copied is so that you do _not_ have to have every page know about all the other pages. – Bryan Oakley Apr 28 '18 at 11:44