如何获取有关我的combobox内线程的信息


import time
import threading
import logging
try:
import tkinter as tk # Python 3.x
import tkinter.scrolledtext as ScrolledText
except ImportError:
import Tkinter as tk # Python 2.x
import ScrolledText
class TextHandler(logging.Handler):
def __init__(self, text):
logging.Handler.__init__(self)
self.text = text
def emit(self, record):
msg = self.format(record)
def append():
self.text.configure(state='normal')
self.text.insert(tk.END, msg + 'n')
self.text.configure(state='disabled')
self.text.yview(tk.END)
self.text.after(0, append)
class myGUI(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.root = parent
self.build_gui()
def build_gui(self):                    
self.root.title('TEST')
self.root.option_add('*tearOff', 'FALSE')
tab_control = ttk.Notebook(self)
tab1 = ttk.Frame(tab_control)
tab_control.add(tab1, text='Main')
tab_control.pack(expand=1, fill='both')
tab2 = ttk.Frame(tab_control)
tab_control.add(tab2, text='Settings')
tab_control.pack(expand=1, fill='both')
tab3 = ttk.Frame(tab_control)
tab_control.add(tab3, text='Logger')
tab_control.pack(expand=1, fill='both')
self.grid(column=0, row=0, sticky='ew')
self.grid_columnconfigure(0, weight=1, uniform='a')
self.grid_columnconfigure(1, weight=1, uniform='a')
self.grid_columnconfigure(2, weight=1, uniform='a')
self.grid_columnconfigure(3, weight=1, uniform='a')
st = ScrolledText.ScrolledText(self, state='disabled')
st.configure(font='TkFixedFont')
st.grid(column=0, row=1, sticky='w', columnspan=4)
text_handler = TextHandler(st)
logging.basicConfig(filename='test.log',
level=logging.INFO, 
format='%(asctime)s - %(levelname)s - %(message)s')        

logger = logging.getLogger()        
logger.addHandler(text_handler)
#CREATED COMBO BOX
self.world_selection = ttk.Combobox(tab1, values=world_list)
self.world_selection.grid(column=0, row=1)
self.world_selection.current(2)
self.world_selection.bind("<<ComboboxSelected>>", get_box_state(self.world_selection))
def worker():
# Skeleton worker function, runs in separate thread (see below)   
while True:
# Report time / date at 2-second intervals
time.sleep(2)
#The question: how do I get my combo box selection here? (can't get to the world_selection variable. world_selection.bind("<<ComboboxSelected>>", get_box_state(self.world_selection)) can't work here.
def main():
root = tk.Tk()
myGUI(root)
t1 = threading.Thread(target=worker, args=[])
t1.start()
root.mainloop()
t1.join()
main()

问题是我无法在线程循环中访问GUI中的变量。但我想知道我的GUI的信息(例如组合框的状态(,但我不知道怎么做。

尝试返回变量,尝试生成全局变量。尝试循环gui,但这只是重置它。

非常感谢任何提示。

请尝试将"world_selection"声明为类变量据我所知,它可以从外部访问

myGUI.world_selection = ttk.Combobox(tab1, values=world_list)

并在"def-worker(("中访问为"myGUI.world_selection">

最新更新