【问题标题】:Error while creating an object of an other class in a class of type Thread in Python在 Python 中的 Thread 类型的类中创建其他类的对象时出错
【发布时间】:2014-03-04 09:21:00
【问题描述】:

级别:初学者

我在 windows 7 32 位上使用 python v2.7 和 wxPython v3.0。

我的应用:我有 3 个课程。一类是gui(wx.Frame),另一类是TestThread(Thread),第三类是labels()

问题:我正在尝试在TestThread(Thread) 类中创建gui(wx.Frame) 类的对象,但出现如下错误:

Traceback (most recent call last):
  File "C:\test\post.py", line 11, in <module>
    class TestThread(Thread):
  File "C:\test\post.py", line 12, in TestThread
    guiObj = gui()
NameError: name 'gui' is not defined

但是,如果我尝试像 wx.CallAfter(gui().createPanels()) 这样从 TestThread(Thread) 类调用 gui(wx.Frame) 类的 createPanels() 类,则会出现以下错误:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
    self.run()
  File "C:\test\post.py", line 24, in run
    wx.CallAfter(gui().createPanels())
TypeError: __init__() takes exactly 4 arguments (1 given)

我认为原因与gui(wx.Frame)__init__() 有关 我不明白原因。

更新:我试图在TestThread(Thread) 类中创建一个labels() 类的对象,我得到与上面第一种情况相同的错误。这个TestThread(Thread) 课程有什么特别之处吗?

下面提供完整代码,可以downloaded here避免识别问题:

#!/usr/bin/env python

from random import randrange
import wx
import wx.lib.scrolledpanel
from threading import Thread
from wx.lib.pubsub import setuparg1
from wx.lib.pubsub import pub as Publisher
##################################################
class TestThread(Thread):
     guiObj = gui()

     def __init__(self):
        Thread.__init__(self)
        self.start()    # start the thread

     def run(self):
        wx.CallAfter(guiObj.createPanels())
        time.sleep(5)

##############################################
class gui(wx.Frame):

    def __init__(self, parent, id, title):
        screenWidth = 800
        screenHeight = 450
        screenSize = (screenWidth, screenHeight)
        wx.Frame.__init__(self, None, id, title, size=screenSize)
        self.locationFont = locationFont = wx.Font(15, wx.MODERN, wx.NORMAL, wx.BOLD)
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer = sizer = wx.BoxSizer(wx.VERTICAL)
        self.panel = panel = wx.lib.scrolledpanel.ScrolledPanel(self, -1, style=wx.SIMPLE_BORDER)
        panel.SetupScrolling()
        panel.SetBackgroundColour('#FFFFFF')
        panel.SetSizer(sizer)
        mainSizer.Add(panel, 15, wx.EXPAND|wx.ALL)
        self.SetSizer(mainSizer)

    def createPanels(self):
       k = 0
       labelObj = labels()
       locations = labelObj.getLabel()
       print locations
       for i in locations:
           sPanels = 'sPanel'+str(k)
           sPanels = wx.Panel(self.panel)
           label = str(k+1)
           text = wx.StaticText(sPanels, -1, label0)
           text.SetFont(self.locationFont)
           text.SetForegroundColour('#0101DF')
           self.sizer.Add(sPanels, 0, wx.ALL, 5)
           self.sizer.Add(wx.StaticLine(self.panel), 0, wx.ALL|wx.EXPAND, 0)
           k += 1

   TestThread()
################################################
class labels():
    def getLabel(self):
       mylist =[]
       i = randrange(10)
       for k in range(1,i+1):
           mylist.append(k)
       return mylist
###############################################

if __name__=='__main__':
   app = wx.App()
   frame = gui(parent=None, id=-1, title="Test")
   frame.Show()
   app.MainLoop()

感谢您的宝贵时间!

【问题讨论】:

    标签: python multithreading python-2.7 user-interface wxpython


    【解决方案1】:

    第一期:

    guiObj = gui()是在Python第一次运行文件的时候执行的,这会导致两个问题:

    • Python 在执行时不知道gui
    • TestThread 的所有实例都将具有相同的 gui 实例

    要修复它们,你必须把guiObj = gui()放在TestThread的构造函数中

    如果你只想修复第一个,请将gui的声明放在TestThread的声明之前。

    第二期:

    使用gui() 是错误的,因为gui 的构造函数需要三个参数(实际上是四个,但第一个是隐式的。你必须用三个参数调用它,就像你在程序末尾所做的那样:@ 987654331@

    【讨论】:

    • 我把guiObj = gui()放在TestThread的构造函数中,并且把gui()的声明放在TestThread()之前。现在我收到错误:NameError: name 'TestThread' is not defined
    • 你为什么把TestThread()放在gui的类声明中,其实?
    • 我对线程主题感到困惑。我只是在做实验。您建议 TestThread() 应该在哪里?我正在尝试使用 TestThread 类来使用线程在我的应用程序中使用 gui 类的 createPanels() 创建面板。我只是想看看它是否会在不冻结/阻塞的情况下更新我的 gui。在我看来,我没有以正确的方式实现它!
    • 你可以在 main 中完成
    猜你喜欢
    • 2017-06-26
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-22
    • 2013-07-23
    • 2012-07-23
    相关资源
    最近更新 更多