我想更改其中一个标签上的配置,但无法弄清楚如何在类外部的函数中引用它。我的目标是创建一个函数,将其放置在循环中以检查“my_turn”的值。根据“my_turn”的值,我想更改“TeamFrame”类中“self.turn”标签的配置,但是我不知道如何在类之外引用它。class LoginScreen(tk.Canvas): def __init__(self, parent): tk.Canvas.__init__(self, parent) self.parent = parent self.create_image(0, 0, image=my_img, anchor="nw") self.button = tk.Button(self.frame, text="Connect", bg='burlywood3', activebackground='burlywood3', font="12", command=lambda: login()) self.button.grid(row=4, columnspan=3, pady=10) def login(): my_name = self.entry.get() msg = str(my_name) client.send(msg.encode(FORMAT)) self.destroy() root.title(f"Main Application - {my_name}") draft = MainApplication(root, my_name) draft.pack(side="top", fill="both", expand=True) threading.Thread(target=start_app).start()class MainApplication(tk.Canvas): def __init__(self, parent, name): tk.Canvas.__init__(self, parent) self.parent = parent self.create_image(0, 0, image=my_img, anchor="nw") self.team = TeamFrame(self, 1, 4, name)class TeamFrame(tk.Frame): def __init__(self, parent, r, c, name): tk.Frame.__init__(self, parent) self.parent = parent self.configure(bd=1, relief="ridge", bg='wheat3') self.grid(row=r, rowspan=2, column=c, padx=10, pady=15, sticky=tk.NE) self.turn = tk.Label(self, bg='wheat3', text=f"You are not on the clock.\n ", fg='gray28', font='bold 12', disabledforeground='red3') self.turn.pack(pady=10)
1 回答
四季花海
TA贡献1811条经验 获得超5个赞
MainApplication您可以将, draft,的实例传递给start_app():
threading.Thread(target=start_app, args=(draft,)).start()
main_app.team.turn然后您可以通过(main_app是 的新参数的名称)访问所需的标签,start_app()如下所示:
def start_app(main_app):
# do something
main_app.team.turn['text'] = 'Hello'
# do other stuff
添加回答
举报
0/150
提交
取消