先写了一个按钮点击槽函数,用这个调用createNewWidget自定义函数时,setCurrentIndex的步骤是不会卡死的,而用printhello函数调用时,明显每次都出现在setCurrentIndex的卡死,该如何理解和解决呢?
def createNewWidget(self):
global widnum
global newPath
print('已有'+ (str)(widnum) + '个窗口,现新建一个')
widgetName = 'Widget' + (str)(widnum)
print('新建窗口名' + widgetName)
exec(widgetName + " = MyWidget()")
print('?')
exec('self.boardLayout.addWidget('+ widgetName + ')')
print('?')# ↓↓ ↓↓ ↓↓ ↓↓ ↓↓ ↓↓ ↓↓ ↓↓出问题 ↓↓ ↓↓ ↓↓ ↓↓ ↓↓ ↓↓ ↓↓ ↓↓ ↓↓
self.boardLayout.setCurrentIndex(widnum)
print('?')
print('当前顶层窗口索引'+(str)(self.boardLayout.currentIndex()))
widnum += 1
newPath = 0
self.printHello()
两个调用它的函数
# 放在一个自定义类的initui方法里
btnNew = QPushButton('新建图层')
btnNew.clicked.connect(self.createNewWidget)
def printHello(self):
global newPath
global lastPath
print('helloworld')
if newPath:
self.createNewWidget()
else:
t = Timer(5, self.printHello)
t.setDaemon(True)
t.start()