在 GUI 中读取文件而不阻塞主标题 [英] Reading a File without block main thead in GUI

查看:29
本文介绍了在 GUI 中读取文件而不阻塞主标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的第一次尝试中,我尝试了,失败了,学习了,并通过以下尝试回来了.

In my first attempt, I tried, failed, learned, and came back with the following attempt.

我有一个文本文件,名称用逗号解析,如下所示:

I have a text file with the names parsed with commas that looks like this:

Ann Marie,Smith,ams@companyname.com

该列表中可能有 100 多个名称.我省略了生成所有其他 GUI 组件的代码,专注于加载组合框和项目.

The list could have over 100+ names in it. I left out the code that generates all the other GUI components to focus on loading the combobox and the items.

我正在尝试在不阻塞主线程的情况下读取文本并将数据加载到组合框中.我查阅了一本教科书和 Python 文档,这就是我想出的.

I'm trying to read a text and load the data into a combobox without blocking the main thread. I consulted a textbook and Pythons documentation, and this is what I came up with.

问题:

在我的定义 read_employees(self,read_file): 中,我返回我读取的数据的 list.我知道 list 不是线程安全的,我在这里使用它的方式可以吗?

In my def read_employees(self,read_file): I return a list of the data I read. I'm aware that list isn't thread-safe, is it okay in the way i used it here?

同样适用于 def textfilemanage(self): 我在该方法中设置组合框,self.combo = wx.ComboBox(self.panel,选择=数据).可以吗?

Same goes for the setting of the combobox in def textfilemanage(self): I set the combobox in that method by doing, self.combo = wx.ComboBox(self.panel, choices=data). Is that okay?

import wx
import concurrent.futures

class Mywin(wx.Frame):

    def __init__(self, parent, title):
        super(Mywin, self).__init__(parent, title=title, size=(300, 200))

        self.panel = wx.Panel(self)
        box = wx.BoxSizer(wx.VERTICAL)
        self.textfilemanage()
        self.label = wx.StaticText(self.panel, label="Your choice:", style=wx.ALIGN_CENTRE)
        box.Add(self.label, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, 20)
        cblbl = wx.StaticText(self.panel, label="Combo box", style=wx.ALIGN_CENTRE)

        box.Add(cblbl, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, 5)

        box.Add(self.combo, 1, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.ALL, 5)
        chlbl = wx.StaticText(self.panel, label="Choice control", style=wx.ALIGN_CENTRE)
        box.AddStretchSpacer()
        self.combo.Bind(wx.EVT_COMBOBOX, self.OnCombo)

        self.panel.SetSizer(box)
        self.Centre()
        self.Show()

    def read_employees(self,read_file):
        emp_list = []
        with open(read_file) as f_obj:
            for line in f_obj:
                emp_list.append(line)
        return emp_list

    def textfilemanage(self):
        filename = 'employees.txt'

        with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
            future_to_read = {executor.submit(self.read_employees, filename)}
            for future in concurrent.futures.as_completed(future_to_read):
                data = future.result()

        self.combo = wx.ComboBox(self.panel, choices=data)

    def OnCombo(self, event):
        self.label.SetLabel("You selected" + self.combo.GetValue() + " from Combobox")


app = wx.App()
Mywin(None, 'ComboBox and Choice demo')
app.MainLoop()

推荐答案

textfilemanage() 中的 for 循环一直等到线程结束,所以不同的线程是无用的在代码中.

The for-loop in textfilemanage() waits until the thread finishes so the different thread is useless in the code.

相反:

__init__中创建空combobox(布局需要)并赋值给self.combo

Create the empty combobox in __init__ (needed for layouting) and assign it to self.combo

textfilemanage() 中启动线程但不要等待它.

In textfilemanage() start the thread but don't wait for it.

read_employees()中用

wx.CallAfter(self.combo.Append, emp_list)

wx.CallAfter(self.combo.AppendItems, emp_list)

(这取决于接受哪​​个变体的 wxPython 版本)

(it depends on the wxPython version which variant is accepted)

这篇关于在 GUI 中读取文件而不阻塞主标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆