等級:初級錯誤而在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()
謝謝您的時間!
我已經把guiObj = gui()放在TestThread的構造函數中,並且在TestThread()之前放置了gui() 。現在我得到的錯誤是: NameError:名稱'TestThread'未定義 –
爲什麼在'gui'的類聲明中放入'TestThread()',實際上? –
我對線程主題感到困惑。我只是在試驗。你在哪裏建議TestThread()應該是?我正在嘗試使用TestThread類來使用線程來使用gui類的createPanels()在我的應用程序中創建面板。我只是想看看它是否更新我的GUI而不會凍結/阻塞。在我看來,我沒有以正確的方式實施它! –