from tkinter import *
class Information()。
def __init__(self, master):
self.master = master
self.load_dict_label()
self.display()
def display(self)。
for i in self.location_and_condition_label:
bg_to_label = self.location_and_condition_label[i]["color"/span>]
row_to_label = self.location_and_condition_label[i]["row"]
column_to_label = self.location_and_condition_label[i]["umn"]
text_to_label = self.location_and_condition_label[i]["text"]
self.i = Label(self.master, font=("Courier", 14), text= text_to_label, bg=bg_to_label) 。 grid(row=row_to_label, column=column_to_label, padx=5, pady=5)
在這個例子中,我選擇了隨機值。
def load_dict_label(self)。
color = "red"
self.location_and_condition_label = {
"0000": {" column": 0, "row": 0, "text": "5:00", "color": color},
"0001": {" column": 1, "行": 0, "text": "Getting up", "color": color},
"0002": {"列": 2, "row": 0, "text": "Wash face", "color": color},
"0003": {"列": 3, "row": 0, "text": "get dress", "color": color},
"0004": {"列": 0, "行": 1, "text": "6:00", "color": color},
"0005": {" column": 1, "row": 1, "text": "喝咖啡", "顏色": color},
"0006": {"列": 2, "行": 1, "text": "閱讀報紙", "顏色": color},
"0007": {"列": 0, "行": 2, "text": "7:00", "color": color},
"0008": {" column": 1, "row": 2, "text": "Turn on tv", "color": color},
"0009": {"列": 2, "行": 2, "text": "喝咖啡", "color": color},
"0010": {"列": 3, "row": 2, "text": "喚醒孩子", "color": color},
"0011": {"列": 4, "row": 2, "text": "organize the
孩子", "color": color},
"0012": {"列": 5, "row": 2, "text": "Make us
早餐", "color": color},
"0013": {" column": 0, "row": 3, "text": "8:00", "color": color},
"0014": {" column": 1, "row": 3, "text": "To go
去作業", "color": color},
"0015": {"列": 2, "row": 3, "text": "Say hello to
每個人", "color": color},
"0016": {" column": 0, "row": 4, "text": "9:00", "color": color},
"0017": {" column": 1, "row": 4, "text": "Make a morning
電話", "color": color},
"0018": {"列": 2, "row": 4, "text": "開始作業", "顏色": color},
"0019": {" column": 3, "row": 4, "text": "喝咖啡", "顏色": color}.
}
我想改變這個函式,而不是產生一個新的函式,我想更新一個舊的函式并顯示在視窗上。
def update_label(self)。
self.location_and_condition_label["0010"]["color"] = "green"。
self.display()
class MainPanel()。
def __init__(self, master):
self._master = master
self._master.configure(background="#929591"/span>)
self._nformation = Information(self._master)
self.running = True[/span]。
def run(self)。
if self.running == True:
self._nformation.update_label()
self._master.after(5000, app.run)
if __name__ == "__main__"/span>:
root = Tk()
app = MainPanel(root)
root.after(5000, app.run)
root.mainloop()
uj5u.com熱心網友回復:
你可以在一個字典中存盤標簽,然后使用configure方法來改變顏色。
def display(self)。
for i in self.location_and_condition_label。
...
label = Label(self.master, font=("Courier", 14), text= text_to_label, bg=bg_to_label)
label.grid(row=row_to_label, column=column_to_label, padx=5, pady=5)
self.labels[i] = label
def update_label(self)。
self.location_and_condition_label["0002"]["color"] = "green"。
self.labs["0002"].configure(background="green")
需要注意的事項:
grid是分開的,所以label被設定為實際的標簽部件self.labelsupdate_label不需要呼叫display,因為display每次被呼叫都會創建新的標簽。相反,我們呼叫特定標簽上的configure方法另外,您可以修改display,只在部件不存在的情況下創建它,然后總是使用configure來將它更新為當前的規格。這個解決方案看起來就像這樣:
def __init__(self, master):
self.master = master
self.labels = {}。 #初始化self.labs為一個空的字典。
self.load_dict_label()
self.display()
def display(self)。
for i in self.location_and_condition_label。
...
if i not in self.labs:
# 創建標簽,如果它還沒有被創建。
label = Label(self.master, font=("Courier", 14)
label.grid(row=row_to_label, column=column_to_label, padx=5, pady=5)
self.labels[i] = label
# 用存盤的配置值更新標簽。
self.labs[i].configure(
text= text_to_label, bg=bg_to_label
)
def update_label(self)。
self.location_and_condition_label["0002"]["color"] = "green"。
self.display()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/319445.html
標籤:
上一篇:串列索引是如何超出范圍的?
