如何获取有关我的combobox内部线程的信息 导入时间 导入线程 导入日志记录 尝试: 将tkinter作为tk#Python 3.x导入 将tkinter.scrolledtext作为scrolledtext导入 除恐怖外: 将Tkinter作为tk#Python 2.x导入 导入滚动文本 类TextHandler(logging.Handler): 定义初始化(self,text): logging.Handler.\uuuuu init\uuuuu(self) self.text=文本 def排放(自我,记录): msg=self.format(记录) 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,追加) 类myGUI(tk.Frame): 定义初始化(自、父、*args、**kwargs): tk.Frame.\uuuuu init\uuuuuu(self,parent,*args,**kwargs) self.root=parent self.build_gui() def构建图形用户界面(自): self.root.title('TEST') self.root.option_add('*tearOff',FALSE') tab_control=ttk.笔记本电脑(自身) tab1=ttk.Frame(制表符控件) tab_control.add(tab1,text='Main') tab_control.pack(expand=1,fill='both') tab2=ttk.Frame(制表符控件) tab_control.add(tab2,text='Settings') tab_control.pack(expand=1,fill='both') tab3=ttk.Frame(制表符控制) tab_control.add(tab3,text='Logger') tab_control.pack(expand=1,fill='both') self.grid(列=0,行=0,sticky='ew') self.grid\u columnconfigure(0,权重=1,统一=a) self.grid\u columnconfigure(1,weight=1,uniform='a') self.grid\u columnconfigure(2,weight=1,uniform='a') self.grid\u columnconfigure(3,weight=1,uniform='a') st=ScrolledText.ScrolledText(self,state='disabled') st.configure(font='TkFixedFont') 标准网格(列=0,行=1,粘性=w',列span=4) text\u handler=TextHandler(st) logging.basicConfig(filename='test.log', 级别=logging.INFO, 格式='(asctime)s-%(levelname)s-%(消息)s') logger=logging.getLogger() logger.addHandler(文本处理程序) #创建组合框 self.world\u selection=ttk.Combobox(表1,值=world\u列表) self.world\u selection.grid(列=0,行=1) 自我。世界_选择。当前(2) self.world\u selection.bind(“,get\u box\u state(self.world\u selection)) def worker(): #骨架辅助函数,在单独的线程中运行(见下文) 尽管如此: #以2秒的间隔报告时间/日期 时间。睡眠(2) #问题:我如何在这里获得我的组合框选择?(无法访问world\u selection变量。world\u selection.bind(“,get\u box\u state(self.world\u selection))无法在此处工作。 def main(): root=tk.tk() myGUI(根目录) t1=线程。线程(目标=工作线程,参数=[]) t1.start() root.mainloop() t1.join() main()
问题是,我无法在线程循环中从GUI访问变量。但我想知道有关GUI的信息(例如,该组合框在任何时候的状态),但我不知道如何访问 尝试返回变量,尝试生成全局变量。尝试循环gui,但只会重置它如何获取有关我的combobox内部线程的信息 导入时间 导入线程 导入日志记录 尝试: 将tkinter作为tk#Python 3.x导入 将tkinter.scrolledtext作为scrolledtext导入 除恐怖外: 将Tkinter作为tk#Python 2.x导入 导入滚动文本 类TextHandler(logging.Handler): 定义初始化(self,text): logging.Handler.\uuuuu init\uuuuu(self) self.text=文本 def排放(自我,记录): msg=self.format(记录) 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,追加) 类myGUI(tk.Frame): 定义初始化(自、父、*args、**kwargs): tk.Frame.\uuuuu init\uuuuuu(self,parent,*args,**kwargs) self.root=parent self.build_gui() def构建图形用户界面(自): self.root.title('TEST') self.root.option_add('*tearOff',FALSE') tab_control=ttk.笔记本电脑(自身) tab1=ttk.Frame(制表符控件) tab_control.add(tab1,text='Main') tab_control.pack(expand=1,fill='both') tab2=ttk.Frame(制表符控件) tab_control.add(tab2,text='Settings') tab_control.pack(expand=1,fill='both') tab3=ttk.Frame(制表符控制) tab_control.add(tab3,text='Logger') tab_control.pack(expand=1,fill='both') self.grid(列=0,行=0,sticky='ew') self.grid\u columnconfigure(0,权重=1,统一=a) self.grid\u columnconfigure(1,weight=1,uniform='a') self.grid\u columnconfigure(2,weight=1,uniform='a') self.grid\u columnconfigure(3,weight=1,uniform='a') st=ScrolledText.ScrolledText(self,state='disabled') st.configure(font='TkFixedFont') 标准网格(列=0,行=1,粘性=w',列span=4) text\u handler=TextHandler(st) logging.basicConfig(filename='test.log', 级别=logging.INFO, 格式='(asctime)s-%(levelname)s-%(消息)s') logger=logging.getLogger() logger.addHandler(文本处理程序) #创建组合框 self.world\u selection=ttk.Combobox(表1,值=world\u列表) self.world\u selection.grid(列=0,行=1) 自我。世界_选择。当前(2) self.world\u selection.bind(“,get\u box\u state(self.world\u selection)) def worker(): #骨架辅助函数,在单独的线程中运行(见下文) 尽管如此: #以2秒的间隔报告时间/日期 时间。睡眠(2) #问题:我如何在这里获得我的组合框选择?(无法访问world\u selection变量。world\u selection.bind(“,get\u box\u state(self.world\u selection))无法在此处工作。 def main(): root=tk.tk() myGUI(根目录) t1=线程。线程(目标=工作线程,参数=[]) t1.start() root.mainloop() t1.join() main(),python,multithreading,user-interface,tkinter,combobox,Python,Multithreading,User Interface,Tkinter,Combobox,问题是,我无法在线程循环中从GUI访问变量。但我想知道有关GUI的信息(例如,该组合框在任何时候的状态),但我不知道如何访问 尝试返回变量,尝试生成全局变量。尝试循环gui,但只会重置它 非常感谢任何提示。请尝试将“world\u selection”声明为类变量 据我所知,它可以从外部访问 import time import threading import logging try: import tkinter as tk # Python 3.x import tkint
非常感谢任何提示。请尝试将“world\u selection”声明为类变量 据我所知,它可以从外部访问
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()
并在“def worker()”中以“myGUI.world_selection”的形式访问
myGUI.world_selection = ttk.Combobox(tab1, values=world_list)