[英]python tkinter button command does not work
我正在嘗試在 python tkinter 中創建一個程序,用戶首先必須登錄,然后關閉第一頁並打開新頁面,其中包含一些按鈕。
按鈕必須在單擊時更改其顏色和文本,但它不執行任何操作
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()
我希望在單擊時更改按鈕顏色和文本,但在單擊它時會發生注釋。
這段代碼有兩個問題:
open_setting_page()
-> 變量b2
和b3
變為None
因為Button(XXX).grid()
返回None
-> 讓按鈕創建和網格放置分為 2 個步驟。button_clicked(button)
function -> Button(button).configure
中是錯誤的。 它應該是button.configure(XX)
來獲取你給 function 的按鈕。下面是這兩個函數的樣子:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.