我需要解决的三个要素,让我的解决办法工作。
1)使用root.after功能,使外部异步事件(如非Tkinter的GUI事件)使用更新的方法来控制GUI元素......谢谢你,特里Jan Reedy!
2)在GUI实例本身作为一种方法执行整体字典更新,而不是尝试使用单独的函数处理我的字典管理。也许这对大多数人来说是显而易见的,但对我来说这是一种范式转变。
3)GUI需要是我的类的所有字典元素的单个实例化,而不是每个元素的单独实例化。
下面的代码片段。
def remote_window_mgr():
remote_window = client_gui()
class client_gui(object):
def __init__(self):
self.root = Tk()
self.root.title('Client Manager')
# Create main Frame in Window
main_frame = ttk.Frame(self.root, borderwidth = 5, relief = 'ridge')
main_frame.grid(column = 0, row = 0, sticky = (N, W, E, S))
main_frame.columnconfigure(0, weight = 1)
main_frame.rowconfigure(0, weight = 1)
# Create Notebook widget to host Client Tabs
self.tab_frame = ttk.Notebook(main_frame)
self.tab_frame.grid(column = 1, row = 1)
self.root.after(1000, func=self.gui_update)
self.root.mainloop()
def gui_update(self):
for client_id in list(Client_Records.keys()):
# Check for new Client
if client_id not in list(Client_Tabs.keys()):
client_data = Client_Records[client_id]
# Create Client Tab
self.add_tab(client_id)
for client_id in list(Client_Tabs.keys()):
# Check if Client Record has been purged...
if client_id not in list(Client_Records.keys()):
# Client Record no longer exists, delete Client Tab
self.remove_tab(client_id)
del Client_Tabs[client_id]
for client_id in list(Client_Tabs.keys()):
# Refresh client tabs for all active clients
self.update_tab(client_id)
self.root.after(1000, func=self.gui_update)
你是对的,你不应该在事件处理程序中连续(或非常长)循环。 tkinter的替代品是使用.after函数。这里有几个关于SO的例子,包括我的一些例子。 –
Found,after,包括你的一个例子。这似乎是我需要在我的GUI对象中刷新的解决方案......只需要将我的范例转换为在对象内部进行字典管理,而不是像我正在尝试的那样在外部进行。谢谢! – w9lp