【发布时间】: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