从课堂上获取价值'来自另一个类python的变量

时间:2017-10-02 15:41:59

标签: python multithreading wxpython

问题:

我正在使用wx.Python为我的应用程序创建一个GUI,它会冻结 按下处理按钮,我试图执行一个线程 使用处理代码,它需要从主类和 还可以设置从线程到主类的值。我已经尝试过我了 从其他帖子看到,但没有他们工作。

Tl; dr,需要获取值并修改 class pls 中的值来自 pNombre 并且修改 modtext

目标:

类线程应该从 class pls 中的用户输入 randomName 并设置 modtext 的值

class TestThread(Thread):

    def __init__(self):
        Thread.__init__(self)
        self.start()


    def run(self):
        #try to get value from variable in the pls class
        randomName = self.pNombre.GetValue()

        for i in range (0,100):
           #this should modify modtext in pls class
           self.modtext.SetValue(str(i))

class pls(wx.Frame):

    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'lilcode', size=(800, 600), style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER ^ wx.MAXIMIZE_BOX)
        panel = wx.Panel(self)
        self.SetBackgroundColour('white')

    mylist = ['2', '3']
    scene = wx.SingleChoiceDialog(None, 'pick', 'pick', mylist)
    if scene.ShowModal() == wx.ID_OK:

        if escena.GetStringSelection() == "2":
            self.pNombre = wx.TextCtrl(panel, style = wx.TE_PROCESS_ENTER, pos=(240,40), size=(300, 20))
            self.pNombre.SetFocus()
            my_sizer.Add(self.pNombre, 0, wx.CENTER)

            self.modtext = wx.TextCtrl(panel, style = wx.TE_READONLY, pos = (50, 280), size = (700, 23))

            start = wx.Button(panel, label="process", pos = (350, 220), size=(70, 23))
            self.Bind(wx.EVT_BUTTON, self.initiate2d, start)
            panel.SetSizer(my_sizer)
            self.Show()

    def initiate2d(self, event):

        TestThread()

1 个答案:

答案 0 :(得分:1)

在与单击按钮时调用的函数相同的类中创建一个线程

无需创建另一个类......

class pls(wx.Frame):

    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'lilcode', size=(800, 600), style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER ^ wx.MAXIMIZE_BOX)
        panel = wx.Panel(self)
        self.SetBackgroundColour('white')

    mylist = ['2', '3']
    scene = wx.SingleChoiceDialog(None, 'pick', 'pick', mylist)
    if scene.ShowModal() == wx.ID_OK:

        if escena.GetStringSelection() == "2":
            self.pNombre = wx.TextCtrl(panel, style = wx.TE_PROCESS_ENTER, pos=(240,40), size=(300, 20))
            self.pNombre.SetFocus()
            my_sizer.Add(self.pNombre, 0, wx.CENTER)

            self.modtext = wx.TextCtrl(panel, style = wx.TE_READONLY, pos = (50, 280), size = (700, 23))

            start = wx.Button(panel, label="process", pos = (350, 220), size=(70, 23))
            self.Bind(wx.EVT_BUTTON, self.initiate2da, start)
            panel.SetSizer(my_sizer)
            self.Show()

    def initiate2d(self):

        for i in range (0,100):
           #this should modify modtext in pls class
           self.modtext.SetValue(str(i))
    def initiate2da(self, event):
        threading.Thread(target = self.initiate2d).start()