0

I'm trying to create a program in python tkinter which user first have to login, then the first page is closed and new one opens with some buttons inside.

button must change it's color and text on click but it does not do anything

from tkinter import *


def button_clicked(button):
    Button(button).configure(bg="red", text="not Active")


def open_setting_page():
    loginPage.destroy()
    setting_page = Tk()
    setting_page.title("Setting")
    setting_page.geometry("400x300")

    b2 = Button(setting_page, text="Active", height=5, width=10, bg="green",command=lambda:button_clicked(b2)).grid(row=0, column=0)
    b3 = Button(setting_page, text="Active", height=5, width=10, bg="green",command=lambda:button_clicked(b3)).grid(row=0, column=1)
    setting_page.mainloop()






#program starts here

loginPage = Tk()
loginPage.title("Security System")
loginPage.geometry("400x300")
Label(text="\n\n\n\n").pack()
l1 = Label(text="Enter Password:")
l1.pack()

password_entry = Entry()
password_entry.insert(0, "Enter Your Password...")
password_entry.pack()
b1 = Button(text="Login", command=open_setting_page)
b1.pack()
loginPage.mainloop()

I want the buttons color and text be changed on click but noting happens when I click it.

3
  • 1
    button_clicked() is creating a brand-new Button, calling .config() on it uselessly, and then throwing the new Button away. If you want to affect the Button that was passed as the button parameter, do button.config(...). Commented Jan 21, 2023 at 20:35
  • 1
    Oh - also you need to assign your actual Buttons to b2/b3, rather than the result of calling .grid() on them. Do the geometry management as a separate statement. Commented Jan 21, 2023 at 20:37
  • Also no matter how many tk.Tk windows you have, you only need 1 .mainloop() so you can remove the setting_page.mainloop() Commented Jan 21, 2023 at 21:02

1 Answer 1

1

there are two problems in this code:

  1. in open_setting_page() -> the variables b2 and b3 become None because Button(XXX).grid() returns None -> lets separate the button creation and the grid placement into 2 steps.
  2. in button_clicked(button) function -> Button(button).configure is wrong. it should be button.configure(XX) to get hold of the button that you gave to the function.

Here is how these two functions could look like:

def button_clicked(button):
    button.configure(bg="red", text="not Active")


def open_setting_page():
    loginPage.destroy()
    setting_page = Tk()
    setting_page.title("Setting")
    setting_page.geometry("400x300")

    b2 = Button(setting_page, text="Active", height=5, width=10, bg="green", command=lambda: button_clicked(b2))
    b3 = Button(setting_page, text="Active", height=5, width=10, bg="green", command=lambda: button_clicked(b3))
    b2.grid(row=0, column=0)
    b3.grid(row=0, column=1)

    setting_page.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.