我嘗試撰寫待辦事項串列。當我單擊按鈕 self.button_check 將顏色從白色條目更改為綠色時,它會起作用,直到我創建第二個條目。問題是帶有新 check.button 的新條目還要查找用于第一個的變數 self.check_var。我現在的問題是:有沒有辦法檢查條目的當前 bg 顏色,更改條目是白色還是綠色而不是使用 self.check_var 變數?
Sry 明顯的錯誤,我剛開始編碼 ??
import tkinter as tk
from tkinter.constants import ANCHOR, CENTER, X
class App():
def __init__(self):
self.window = tk.Tk()
self.window.title("To-Do-List")
self.window.geometry("700x700")
self.x_but, self.y_but = 0.05, 0.2
self.x_ent, self.y_ent = 0.05, 0.2
self.x_but2 = 0.3
self.check_var = True
self.start_frame()
self.grid1()
self.window.mainloop()
def start_frame(self):
self.label1 = tk.Label(text="To Do List", font=("", 30))
self.label1.place(relx=0.5, rely=0.05, anchor=CENTER)
def grid1(self):
self.button1 = tk.Button(text="Create", command= self.create_field)
self.button1.place(relx = self.x_but, rely= self.y_but)
def create_field(self):
self.y_but = 0.05
self.button1.place(relx= self.x_but, rely= self.y_but)
self.entry1 = tk.Entry()
self.entry1.place(relx= self.x_ent, rely= self.y_ent)
x = self.entry1
self.button_check = tk.Button(text="?", height= 1,command=lambda x=self.entry1: self.check(x))
self.button_check.place(relx= self.x_but2, rely=self.y_ent)
self.y_ent = 0.05
def check(self,ent):
if self.check_var:
ent.configure(bg="Green")
self.check_var = False
else:
ent.configure(bg="White")
self.check_var = True
app = App()
uj5u.com熱心網友回復:
您可以將值分配給它Entry本身:
def create_field(self):
self.y_but = 0.05
self.button1.place(relx= self.x_but, rely= self.y_but)
# you don't need to use `self.` here necessarily, the instance attribute will get
# overwritten anyways when you create a new `Entry`, same for button below
entry = tk.Entry()
entry.place(relx= self.x_ent, rely= self.y_ent)
# create an attribute for the `Entry` and set it to True
# when you pass the `Entry` to the function, you will be able to
# access this attribute too
entry.check_var = True
button_check = tk.Button(text="?", height= 1,command=lambda x=entry: self.check(x))
button_check.place(relx= self.x_but2, rely=self.y_ent)
self.y_ent = 0.05
然后改變你的check方法:
# add this decorator because the method doesn't require `self` anymore
@staticmethod
def check(ent):
# here you can access the `Entry`'s attribute `check_var`
# which was created in the other method
if ent.check_var:
ent.configure(bg="Green")
ent.check_var = False
else:
ent.configure(bg="White")
ent.check_var = True
此外,我建議您使用pack而不是place,因為在這種情況下使用起來要容易得多(現在可能更難適應)。并且您不需要使用 from 的常量tkinter,它們只包含一個同名的字串,因此您可以輕松地使用該字串來代替,例如:anchor='center'。
uj5u.com熱心網友回復:
我認為您在這里可以做的是,check_var您可以將條目物件存盤check_var為字典中的鍵和值,而不是使用全域變數。這樣,由于它是一個待辦事項應用程式,您可以通過使用條目物件對字典進行索引來輕松獲取該值。
class App():
def __init__(self):
....
self.info = {} # Create a dictionary
def create_field(self):
.....
self.entry1 = tk.Entry()
self.entry1.place(relx= self.x_ent, rely= self.y_ent)
self.button_check = tk.Button(text="?", height= 1,command=lambda x=self.entry1: self.check(x))
self.button_check.place(relx= self.x_but2, rely=self.y_ent)
self.info[self.entry1] = False # Add the item to the dictionary
def check(self,ent):
check_var = self.info[ent] # Index the needed item from the dictionary
if not check_var:
ent.configure(bg="Green")
self.info[ent] = True # Update the flag
else:
ent.configure(bg="White")
self.info[ent] = False
通過這種方式,如果您以后想擴展應用程式,您只需將更多資訊添加到與給定任務相關的字典中,例如任務文本本身。
uj5u.com熱心網友回復:
檢查這篇文章的第一個答案:Changing entry box background color in tkinter
他繼續介紹如何根據物體的內容來選擇物體的背景顏色。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/400217.html
上一篇:影像保存到錯誤的檔案路徑
