Home > OS >  Make A Button That Opens Another Window in Tkinter
Make A Button That Opens Another Window in Tkinter

Time:10-17

I have 2 windows. GUI and Information

I want to make a button on GUI that opens up the window Information, but I don't know how to do that.

I tried to use the TopLevel widget but I don't know how to implement it correctly.

This is the code for GUI

from tkinter import *
from automata.fa.dfa import DFA
from DFA import arranca


def btn_clicked():
    print("Button Clicked")
    
def submit():
    canvas.itemconfig(Canv, text="\t" arranca(entry1.get()))

window = Tk()

window.geometry("880x550")
window.configure(bg = "#ffffff")
canvas = Canvas(
    window,
    bg = "#ffffff",
    height = 550,
    width = 880,
    bd = 0,
    highlightthickness = 0,
    relief = "ridge")
canvas.place(x = 0, y = 0)

This the code for Information

from tkinter import *

window = Tk()

window.title("Information")
window.geometry("700x400")
window.configure(bg = "#ffffff")
canvas = Canvas(
    window,
    bg = "#ffffff",
    height = 400,
    width = 700,
    bd = 0,
    highlightthickness = 0,
    relief = "ridge")
canvas.place(x = 0, y = 0)

background_img = PhotoImage(file = f"background_1.png")
background = canvas.create_image(
    350.0, 188.0,
    image=background_img)

window.resizable(False, False)
window.mainloop()

CodePudding user response:

This code demonstrates how to create a Tk window with button and a Toplevel child window.

All you have to do is populate these windows with your preferred widgets.


import tkinter as tk

class GUI:

    def __init__(self):
        self.parent = tk.Tk()
        self.parent.title("GUI Window")
        # Button control
        button = tk.Button(
            self.parent, text = "New window", command = self.window)
        button.grid(sticky = tk.NSEW)
        # Define window size
        self.parent.geometry("278x26")

    def window(self):
        try:
            self.information.winfo_viewable()
        except AttributeError as err:
            self.information = tk.Toplevel(self.parent)
            # Make Toplevel a child of parent
            self.information.transient( self.parent )
            self.information.title("Information Window")

            # place all your widgets here

        else:
            self.information.focus_force()

if __name__ == "__main__":
    app = GUI()
    app.parent.mainloop()
  • Related