anshul shrivastava
anshul shrivastava

Reputation: 11

load new frame/window while clicking the button with tkinter

i have been making a tkinter program in which i have to load new frame/window while clicking the button

from tkinter import *
import os
import tkinter.messagebox as tm

t=Tk()
t.title('Gym Management')
t["bg"]="black"

#declaration of frame
top=Frame(t)
top.grid(row=0,columnspan=2)
left=Frame(t)
left.grid(row=1,column=0)
right=Frame(t,bg="black")
right.grid(row=1,column=1)

#top frame
l1=Label(top,text="Silver`s Gym",fg="yellow",bg="black",font="impact 50 bold",pady=30)
l1.grid(sticky=E)

#left frame
img1=PhotoImage(file="1.png")
i1=Label(left,image=img1,padx=10)
i1.grid(row=0,column=0,sticky=E)

#right frame
l2=Label(right,text="USERNAME",fg="white",bg="black",padx=10,width=10)
l2.grid(row=0,column=0)
l3=Label(right,text="PASSWORD",fg="white",bg="black",padx=10,width=10)
l3.grid(row=1,column=0)
e2=Entry(right)
e2.grid(row=0,column=1)
e3=Entry(right,show="*")
e3.grid(row=1,column=1)
checkbox = Checkbutton(right, text="Keep me logged in",fg="white",bg="black")
checkbox.grid(columnspan=2)

# defining login button
def login_btn_clickked():
        enter code here`#print("Clicked")
        username = e2.get()
        password = e3.get()

        #print(username, password)

        if username == "john" and password == "password":
            tm.showinfo("Login info", "Welcome John")
        else:
            tm.showerror("Login error", "Incorrect username")
#defining signup button
def sugnup_btn_clickked():
    filename = 'test.py'
    os.system(filename) #Open file [Same as Right-click Open]
    os.system('notepad '+filename)

logbtn = Button(right, text="Login", command = login_btn_clickked,width=15,padx=2)
logbtn.grid(row=3,column=0)
signup = Button(right, text="sign up", command = sugnup_btn_clickked,width=15,padx=2)
signup.grid(row=3,column=1)
t.mainloop()

what should i add to do it? i haven`t learn the concept of tk so please explain it with the t the way i have done

Upvotes: 0

Views: 7176

Answers (1)

Ethan Field
Ethan Field

Reputation: 4730

Please be very, very careful when using the word frame and the word window when talking about tkinter.

They are so very, very distinct from each other.

Frame widgets in tkinter are essentially "boxes" which you put things in. They can be used to separate different widgets into their own containers in a window similar (although not identical) to the way in which divs work in HTML.

There are two distinct types of "window" in tkinter Tk() windows and Toplevel widgets.

The key difference is that you shouldn't ever have more than one Tk() window this answer explains why this is the case. Toplevel widgets on the other hand can be used whenever and wherever you want and should be used as "extra windows" whenever your application needs to have more than one window.

The below script shows how you can use buttons to create both Frames and Toplevels where the new Frame widgets are just empty red squares and the new Toplevel widgets are just empty windows

from tkinter import *

root = Tk()

def frame():
    Frame(root, bg="red", width=100, height=100).pack(padx=5, pady=5)

def window():
    Toplevel(root)

framebutton = Button(root, text="Frame", command=frame)
framewindow = Button(root, text="Window", command=window)

framebutton.pack()
framewindow.pack()

root.mainloop()

Upvotes: 3

Related Questions