how to change the frame when a button is clicked

0 Ash Marz · October 14, 2015
Hello Guys, I tried many ways and I even looked at stackoverflow and I did get the answer but I dont understand how to do it.. So can you please help me how can I change the text from a frame when a button is clicked for example.... If I click the "Log In" button the frame should transition to the next frame which says "Log In Successful" but I understand how to do it ... here is my code.. 



from tkinter import *


class FirstFrame():
    def __init__(self):
        f1 = Frame(self)
        label = Label(f1, text="Submit successful")
        label.pack() 


class MainWindow:
    def __init__(self):
        window = Tk()
        frame = Frame(window)
        frame.pack(expand=True)
        btframe = Frame(window)
        btframe.pack(side=BOTTOM, expand=True)
        
        self.intro = Label(frame, text="hello", font =("Courier", 20, "bold"))
        self.name = Label(frame, text="Name",foreground="red", font=("Helvetica",16,"bold"))
        self.password = Label(frame, text="Password", foreground="blue", font=("Helvetica",16,"bold"))

        self.entry1 = Entry(frame)
        self.entry2 = Entry(frame)

        self.intro.grid(row=0, columnspan=2)
        self.name.grid(row=1, column=0)
        self.entry1.grid(row=1, column=1)
        self.password.grid(row=3, column=0) 
        self.entry2.grid(row=3, column=1)


        self.submitB = Button(btframe, text="Submit", command=FirstFrame())
        self.quitB = Button(btframe, text="Quit", command=window.destroy)
        self.submitB.pack()
        self.quitB.pack()

    
        window.mainloop()

MainWindow()

Post a Reply

Replies

Oldest  Newest  Rating
0 sfolje 0 · October 16, 2015
Sorry for ignoring your code, but it returns error.
Another advice in tkinter: DO NOT use pack() and grid() together.

So ignoring your code, below I (in fact i copy-pasted code from my school class) made simple button, that changes text. ( Pls take care of indentation, i am too lazy.)

## take care of indentation, if it turns error

from tkinter import *

class Status():
def __init__(self, master):


# Status at the beginging is 'nothing to see here.'
self.status = 'nothing to see here.'

# Instead of Frame() and pack() we use grid()

# CAUTION: If you use pack() and grid() together, then Tkinter will
# be "calculating" infinitely about positioning of the elements!

button_submit = Button(master, text="Submit", command=self.submit)
button_submit.grid(row=0, column=0)

self.text_status = StringVar(value=str(self.status))
label_status = Label(master, textvariable=self.text_status)
label_status.grid(row=2, column=2)

def submit(self):
self.status = 'button submited your thingy'
self.text_status.set(str(self.status))

root = Tk()

app = Status(root)

root.mainloop()

If it helps, i can give you 2 more tkinter basics examples: entry that changes some text instantly, when u type in it.
  • 1

Python

107,004 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator