簡體   English   中英

Python Tkinter 用於唯一檢查的共享變量

[英]Python Tkinter shared variable for unique check

我嘗試編寫待辦事項列表。 當我單擊按鈕 self.button_check 將顏色從白色條目更改為綠色時,它會起作用,直到我創建第二個條目。 問題是帶有新 check.button 的新條目還查找用於第一個的變量 self.check_var。 我現在的問題是:有沒有辦法檢查條目的當前 bg 顏色,改變條目是白色還是綠色,而不是使用 self.check_var 變量?

抱歉有明顯的錯誤,我剛開始編碼

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()

您可以將值分配給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 variable 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 ,因為在這種情況下使用起來更容易。 而且您不需要使用tkinter中的常量,它們只包含一個相同名稱的字符串,因此您可以輕松地使用該字符串來代替: anchor='center'

我認為您可以在這里做的不是使用全局check_var而是將條目名稱和check_var作為鍵和值存儲在字典中。 這樣,由於它是一個待辦事項應用程序,您可以通過使用條目 object 對其進行索引來輕松獲取值。

class App():

    def __init__(self):
        ....
        self.info = {}
    
    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
    def check(self,ent):
        check_var = self.info[ent]

        if not check_var:
            ent.configure(bg="Green")
            self.info[ent] = True

        else:
            ent.configure(bg="White")
            self.info[ent] = False

檢查此帖子的第一個答案: 更改 tkinter 中的輸入框背景顏色

他繼續講述如何根據實體的內容來判斷實體的背景顏色。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM