2014-03-04 73 views
0
在類類型的線程創建其它類的一個對象

等級:初級錯誤而在Python

我使用蟒V2.7和Windows 7 32位的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 

但是,如果我嘗試調用gui(wx.Frame)createPanels()TestThread(Thread)類這樣wx.CallAfter(gui().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) 

我THI原因是有關__init__()gui(wx.Frame) 我不明白原因。

更新:我試圖在TestThread(Thread)類創建labels()類的一個對象,我得到同樣的錯誤,如圖第一殼體的上方。這個TestThread(Thread)班有什麼特別的嗎?

的完整代碼下面提供並且可以downloaded here避免identation問題:

#!/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() 

謝謝您的時間!

回答

1

對於第一個問題:

guiObj = gui()時Python會首先運行該文件,這會導致兩個問題是執行:

  • 執行時,它
  • 所有實例Python不知道guiTestThread將具有相同的gui實例

要解決他們兩個,你必須把guiObj = gui()TestThread

構造函數如果你只是想解決的第一個,放gui申報TestThread聲明之前。

對於第二個問題:

使用gui()是錯誤的,因爲gui構造函數有三個參數(4實際,但第一個是隱含的。你必須用三個參數來調用它,就像你在程序結束時所做的那樣:gui(parent=None, id=-1, title="Test")

+0

我已經把guiObj = gui()放在TestThread的構造函數中,並且在TestThread()之前放置了gui() 。現在我得到的錯誤是: NameError:名稱'TestThread'未定義 –

+0

爲什麼在'gui'的類聲明中放入'TestThread()',實際上? –

+0

我對線程主題感到困惑。我只是在試驗。你在哪裏建議TestThread()應該是?我正在嘗試使用TestThread類來使用線程來使用gui類的createPanels()在我的應用程序中創建面板。我只是想看看它是否更新我的GUI而不會凍結/阻塞。在我看來,我沒有以正確的方式實施它! –