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.
there are two problems in this code:
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.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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.