0

So I'm trying to make a login app and want to add register option to it... I made a register button and I want it to switch from the login page to the register page without opening any new window (I'm basicly want it to switch between frame1 to frame2 and erase frame1's widgets and display frame2's widgets) the problem is that I'm new to tkinter and to python in general and Couldn't find a proper solution to it... here is a sample of my code that displays the tkinter pages (sorry if its a little long one... I've tried to make it as readable as possible so you would understand)

import tkinter as tk
from tkinter import *
from tkinter import ttk


class app:
    def __init__(self, master):
        self.frame1 = Frame(root, width=300, height=300)
        self.frame1.pack()
        self.reg_txt = ttk.Label(root, text='page1')
        self.reg_txt.place(x=88, y=30)
class app2:
    def __init__(self, master):
        self.frame2 = Frame(root, width=300, height=300)
        self.reg_txt2 = ttk.Label(root, text='page2')
        self.reg_txt2.place(x=88, y=30)
        self.frame2.pack()
    
root = Tk()
a = app(root)
a = app2(root)
root.mainloop()
roee1454
  • 33
  • 1
  • 6
  • use frames, there are multiple examples here on SO on how to do that, search google, make classes that inherit from Frames, create some controller class if you need, frame changes have been answered here before, also provide a [mre] of what you want to do – Matiiss Sep 06 '21 at 19:48
  • [Here's](https://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter/7557028#7557028) a popular way to do it. – martineau Sep 06 '21 at 21:59

2 Answers2

0

You can structure your code like this, where you declare different methods on the app class that start by destroying all existing frames, then build new ones. The __init__ function calls one of the methods to start things off, then pressing buttons calls the methods after that.

from tkinter import *
from tkinter import ttk


class app:
    def __init__(self, master):
        self.master = master
        self.master.geometry("200x200")
        self.login()
    
    def login(self):
        for i in self.master.winfo_children():
            i.destroy()
        self.frame1 = Frame(self.master, width=300, height=300)
        self.frame1.pack()
        self.reg_txt = ttk.Label(self.frame1, text='login')
        self.reg_txt.pack()
        self.register_btn = ttk.Button(self.frame1, text="Go to Register", command=self.register)
        self.register_btn.pack()
    
    def register(self):
        for i in self.master.winfo_children():
            i.destroy()
        self.frame2 = Frame(self.master, width=300, height=300)
        self.frame2.pack()
        self.reg_txt2 = ttk.Label(self.frame2, text='register')
        self.reg_txt2.pack()
        self.login_btn = ttk.Button(self.frame2, text="Go to Login", command=self.login)
        self.login_btn.pack()

root = Tk()
app(root)
root.mainloop()
schwartz721
  • 767
  • 7
  • 19
0

you can create a fixe Frame in the top or the left that will be navbar . this navbar will contains items two button login and sign up on click one of these buttons you can change the Frame of center if you click login on navbar the Frame of login will be appear and vice versa

Med
  • 73
  • 9