【发布时间】:2014-06-12 21:26:48
【问题描述】:
我目前正在设计一个基于 UI 的软件。
我创建了第一个 Python 脚本,用作我的主要源代码。
下面是我的脚本
import OneTouchToolLogs
import UserInterface
import wx
import XML_Parse
import threading
if __name__ == '__main__':
#Init the logging - Tools and also logcat scenario
Logs = OneTouchToolLogs.Logging()
LogFileName = Logs.LogFileOpen()
Logs.LogMessage(LogFileName, "LOG" , "Starting OneTouchAutomationTools")
#Initialized User Interface
Logs.LogMessage(LogFileName, "LOG" , "Loading user interface")
app = wx.App()
frame = UserInterface.UserInterface(None, -1, 'OneTouchAutomation')
app.MainLoop()
我创建了另一个包含 UserInterface 类的 python 文件以使其更清晰。
新的python类如下:
import wx
class UserInterface(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent,id, title)
self.parent = parent
self.worker = None
self.initialize()
def initialize(self):
menubar =wx.MenuBar()
#CREATE FILE MENU SECTION
fileMenu = wx.Menu()
fileMenu.Append(wx.ID_NEW, '&New suites\tCTRL+N')
fileMenu.Append(wx.ID_OPEN, '&Open suites\tCTRL+O')
通过设计,我已经完成,UI 成为整个执行的阻塞点,因为它不是在线程上完成的。
我已经修改了我的主脚本来替换
app.MainLoop()
通过
t = threading.Thread(target=app.MainLoop)
t.setDaemon(1)
t.start()
结果是线程创建得很好但他在一秒钟内被杀死。我只看到窗户,它已经关闭了。
任何人都知道我能够使用我的 UserInterface 类创建此接口并在线程中启动它以允许主程序继续运行?
【问题讨论】:
-
你所有的 gui 都需要在你的主线程中处理...... wx 文档在几个地方提到了这一点 afaik
-
这可能会有所帮助:stackoverflow.com/a/23425882/2382792
-
我想我应该改变我的设计方式。我认为我的用户界面将是我的主要 python 脚本。
标签: python multithreading wxpython