skip to Main Content

so , I was working on an app using "customtkinter" and wanted to know if it is possible to create multi window app ?

when I searched about it the only method I found was "top level window" but the problem is that I don’t want to create a new window , I want multiple kind of ‘layers’ guess u could say in the app like I want everything to be on the screen no new window for example – lets say an app so I want login window to appear at start after login or registration I don’t want a new window to open I want the content of app to appear in same window like any normal app nowadays in mobile or pc , so is it possible in python custom tkinter or not ?? if it isn’t can anyone suggest what language or software should I use for it I searched about react and node , react native as well android studio etc which should I use if it isn’t possible in python .

2

Answers


  1. This can be done. Following is an example program containing three custom tkinter window. The first one has name entry field. The second window has address entry field. and a button. after entering, name, address, when button is pressed, the name and address will be displayed in the third window.

    import customtkinter as ct
    
    def getdata():
        got_name = name_entry.get()
        got_address = address_entry.get()
        first_line.configure(text = got_name)
        second_line.configure(text = got_address)
        
    def focus_add(e): address_entry.focus_set()
    def btn_focus(e): btn.focus_set()
    
    
    root = ct.CTk()
    root.geometry('400x300+100+100')
    name = ct.CTkLabel(root, text = 'Name')
    name.pack()
    
    name_entry = ct.CTkEntry(root)
    
    name_entry.pack()
    
    
    
    window = ct.CTk()
    window.geometry('400x300+500+0')
    
    address =ct.CTkLabel(window, text='Address')
    address.pack()
    
    address_entry = ct.CTkEntry(window)
    address_entry.pack()
    
    btn = ct.CTkButton(window, text='Get Details', command = getdata)
    btn.pack()
    
    name_entry.bind('<Leave>',focus_add)
    address_entry.bind('<Leave>', btn_focus)
    
    wnd= ct.CTk()
    wnd.geometry('400x300+900+0')
    
    first_line = ct.CTkLabel(wnd,text='')
    first_line.pack()
    second_line = ct.CTkLabel(wnd,text='')
    second_line.pack()
    
    root.mainloop()
    
    Login or Signup to reply.
  2. To create an app that has multiple pages / layers, you have 2 ways to solve this issue:

    1. Each time you want to show a new page, you destroy every widget on the window before placing new ones, for example (win is the root window):
    # Destroying every widget
    for widget in win.winfo_children():
        widget.destroy()
    
    # Creating new widgets
    label = CTkLabel(win, text="I'm on a new page")
    label.pack()
    
    1. The solution I use (maybe more complicated but allows to go back to previous pages more easily): make a class for each page and make a show() and hide() method for each class. Each time you want to change page, you call the hide() method of each page and call the show() method of the page you want to show. For example:
    from customtkinter import *
    
    win = CTk()
    win.geometry("300x300")
    
    
    def hide_all():
        """ Hides all the pages """
        page1.hide()
        page2.hide()
    
    
    def show_page1():
        page1.show()
    
    
    def show_page2():
        page2.show()
    
    
    class Page1:
        def __init__(self):
            self.label1 = CTkLabel(win, text="I'm on the first page")
            self.button1 = CTkButton(win, text="Change to the second page", command=show_page2)
    
        def show(self):
            hide_all()
            self.label1.pack()
            self.button1.pack()
    
        def hide(self):
            self.label1.pack_forget()
            self.button1.pack_forget()
    
    
    class Page2:
        def __init__(self):
            self.label1 = CTkLabel(win, text="I'm on the second page")
            self.button1 = CTkButton(win, text="Change to the first page", command=show_page1)
    
        def show(self):
            hide_all()
            self.label1.pack()
            self.button1.pack()
    
        def hide(self):
            self.label1.pack_forget()
            self.button1.pack_forget()
    
    
    page1 = Page1()
    page2 = Page2()
    
    page1.show()
    
    win.mainloop()
    

    Hope I helped you, have a nice day

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search