Home > Back-end >  How to open two modules in one window, Python Tkinter
How to open two modules in one window, Python Tkinter

Time:09-13

I have a question: i am tossing the code from 2ch files, i have already lost ideas. Calling fileA.py opens a window for me with two buttons exit and start. Exit works but when I click start I need to open the second window fileB.pt. (I want both windows to open in one window) Seemingly works only problem I have is it doesn't open "window on window" but "docks underneath" and I have the effect of two windows open :/. Please help, thank you in advance:) Python 3.10

fileA.py

import tkinter as tk

from GUI.module.scale_of_img import get_scale

class FirstPage:

    def __init__(self, root):
        self.root = root

    def get_settings(self):
        # Window settings
        self.root.title('....')
        self.root.resizable(False, False)
        self.root.geometry("1038x900")

    def get_picture(self):
        # Background loading and setting parameters
        self.bg = tk.PhotoImage(file="....png")
        self.logo = tk.PhotoImage(file="...logo.png")

        # Background image settings
        self.my_canvas = tk.Canvas(self.root, width=1038, height=500)
        self.my_canvas.pack(fill="both", expand=True)
        self.my_canvas.create_image(0, 150, image=self.bg, anchor="nw")
        self.my_canvas.create_image(0, 800, image=self.logo, anchor="nw")

    def get_text(self):
        title = """..."""

        describe = """..."""

        # Text on page
        self.my_canvas.create_text(500, 80,
                                   text=title,
                                   font=("Helvetica", 30),
                                   justify='center', fill="red")

        self.my_canvas.create_text(500, 650,
                                   text=describe,
                                   font=("Helvetica", 10),
                                   justify='center')

    def get_second_page(self):
        from GUI.module.second_page import SecondPage
        SecondPage(self.root).get_run()

    def get_button(self):
        # Add buttons
        button1 = tk.Button(self.root, text="Start", command=self.get_second_page)
        button2 = tk.Button(self.root, text="Exit", command=self.root.destroy)

        # Dimensions and location of buttons
        self.my_canvas.create_window(200, 300, anchor="nw", window=button1, height=50, width=200)
        self.my_canvas.create_window(650, 300, anchor="nw", window=button2, height=50, width=200)

    def get_run_first_page(self):
        # Launching the application
        self.get_settings()
        self.get_picture()
        self.get_text()
        self.get_button()
        get_scale()
        self.root.mainloop()


if __name__ == '__main__':
    first = FirstPage(tk.Tk())
    first.get_run_first_page()

fileB.py

import tkinter as tk

"importy..."

''' The second side of the application '''

class SecondPage:

    def __init__(self, root=None):
        self.root = root
        self.my_canvas = tk.Canvas(self.root, width=1038, height=678)
        self.my_canvas.pack(fill="both", expand=True)

    # def get_settings(self):
    #     # Window settings
    #     self.root.title('....')
    #     self.root.resizable(False, False)
    #     self.root.geometry("1038x900")

    def get_picture(self):
        # Background loading and setting parameters
        self.bg = tk.PhotoImage(file="...background.png")
        self.logo = tk.PhotoImage(file="...logo.png")

        # Background image settings
        self.my_canvas.create_image(0, 150, image=self.bg, anchor="nw")
        self.my_canvas.create_image(0, 800, image=self.logo, anchor="nw")


    def get_text(self):
        # Program title
        title = """..."""

        self.my_canvas.create_text(520, 80,
                                   text=title,
                                   font=("Helvetica", 30),
                                   justify='center', fill="red")

    def get_aaa(self):
        ThirdPage....(tk.Toplevel()).get_run()

    def get_bbb(self):
        ThirdPage....(tk.Toplevel()).get_run()

    def get_ccc(self):
        ThirdPage....(tk.Toplevel()).get_run()

    def get_button(self):
        # Add buttons
        button1 = tk.Button(self.root, text="...", command=self.aaa)
        button2 = tk.Button(self.root, text="...", command=self.bbb)
        button3 = tk.Button(self.root, text="...", command=self.ccc)
        button4 = tk.Button(self.root, text="Exit", command=self.root.destroy)

        # Dimensions and location of buttons
        self.my_canvas.create_window(100, 380, anchor="nw", window=button1, height=200, width=200)
        self.my_canvas.create_window(430, 380, anchor="nw", window=button2, height=200, width=200)
        self.my_canvas.create_window(750, 380, anchor="nw", window=button3, height=200, width=200)
        self.my_canvas.create_window(0, 100, anchor="nw", window=button4, height=50, width=200)

    def get_run(self):
        # Launching the application
        # self.get_settings()
        self.get_picture()
        self.get_text()
        self.get_button()


if __name__ == '__main__':
    second = SecondPage(tk.Tk())
    second.get_run()

CodePudding user response:

assuming what you want is another Tk window to open, you shouldn't give it the same root, instead use an instance of Toplevel

from tkinter import Toplevel

# class definition here

    def get_second_page(self):
        from GUI.module.second_page import SecondPage
        SecondPage(Toplevel(self.root)).get_run()

passing the Toplevel as a child of self.root is necessary, but note that the two windows have different roots.

Edit: turns out this wasn't what the OP ment by "window on window" -_-, but it am keeping it here for other readers.

CodePudding user response:

in order to put two "windows" in the same "window" you need to put all items inside a Frame, which is basically a container than you can simply pack when you want everything to show and unpack when you want everything in it to disapear.

all items in the first window will be children of a frame and all items in the second window will be children of another frame, and to switch you just need to call pack_forget() on one and pack() on another.

for the first file

class FirstPage:

    def __init__(self, root):
        self.root = root
        self.frame = tk.Frame(root)
        self.frame.pack(expand=True)

    def get_picture(self):
        # all items inside this window must be children of self.frame
        self.my_canvas = tk.Canvas(self.frame, width=1038, height=500)
        ...

    def get_second_page(self):
        from GUI.module.second_page import SecondPage
        self.frame.pack_forget() # to hide first page
        # self.frame.destroy()  # if you are never brining it back
        SecondPage(self.root).get_run()

and for the second file

class SecondPage:

    def __init__(self, root=None):
        self.root = root
        self.frame = tk.Frame(root) # new frame
        self.frame.pack(expand=True)
        self.my_canvas = tk.Canvas(self.frame, width=1038, height=678)
        self.my_canvas.pack(fill="both", expand=True)

    def get_button(self):
        # Add buttons
        # all here should be children of self.frame now
        button1 = tk.Button(self.frame, text="...", )
        ...

you could destroy the first frame when you switch over to save some resources if you don't intend to return to it ever again, but the difference in memory is negligible.

  • Related