【问题标题】:How to get information about my combobox inside thread如何在线程内获取有关我的组合框的信息
【发布时间】:2020-12-22 03:19:30
【问题描述】:
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,但这只是重置它。

非常感谢任何提示。

【问题讨论】:

标签: python multithreading user-interface tkinter combobox


【解决方案1】:

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

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

在“def worker()”中作为“myGUI.world_selection”访问

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    相关资源
    最近更新 更多