我的GUI中有两个标签,每个标签对应不同的窗口。我想选择此标签,并使用GPIOs将主窗口更改为新窗口。 代码可以工作,但存在两个问题:
1-有时使用setStyleSheet更改标签的背景颜色不起作用(两个标签的颜色相同,但其中一个应该是黑色,另一个应该是蓝色)。当这种情况发生时,我需要移动GUI窗口,然后就可以了)
2-更重要的问题:有时更改标签颜色或选择标签后打开新窗口会导致分割错误
任何人都知道如何改进代码来解决这些问题。谢谢
下面是main.py代码:
class mainWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
super(mainWindow,self).__init__(parent)
self.InitUI()
def InitUI(self):
GPIO1.setmode(GPIO1.BCM)
GPIO1.setup(self.gpio_plus, GPIO1.IN, pull_up_down=GPIO1.PUD_UP)
GPIO1.setup(self.gpio_enter, GPIO1.IN, pull_up_down=GPIO1.PUD_UP)
GPIO1.add_event_detect(self.gpio_plus, GPIO1.FALLING, callback=self.Gpio_plus_catchButton, bouncetime=100)
GPIO1.add_event_detect(self.gpio_enter, GPIO1.FALLING, callback=self.Gpio_enter_catchButton, bouncetime=100)
self.resize(480,272)
self.timer = PyQt5.QtCore.QTimer(self)
self.timer.timeout.connect(self.updateStatus)
self.timer.start(1000)
self.Label1 = QLabel(self)
self.Label1.setText('windows1')
self.Label1.move(150,160)
self.Label1.setStyleSheet("font:bold;font-size:24px")
self.Label2 = QLabel(self)
self.Label2.setText('windows2')
self.Label2.move(150,160)
self.Label2.setStyleSheet("font:bold;font-size:24px")
self.change=1
self.winNum=0
self.Label1.setStyleSheet("font-size:24px;color: blue;font:bold")
def Gpio_plus_catchButton(self,btn): #GPIO
time.sleep(0.05)
if (GPIO1.input(self.gpio_plus)) == GPIO1.LOW:
if(self.change==1):
self.MenuLabel.setStyleSheet("font-size:24px;color: blue;font:bold")
self.startLabel.setStyleSheet("font-size:24px;color: black;")
else:
self.startLabel.setStyleSheet("font-size:24px;color: blue;font:bold")
self.MenuLabel.setStyleSheet("font-size:24px;color: black;")
self.change=-self.change
return
def Gpio_enter_catchButton(self,btn): #GPIO
time.sleep(0.05)
if (GPIO1.input(self.gpio_enter)) == GPIO1.LOW:
if(self.change==1):
self.winNum=-1
elif(self.change==-1):
self.winNum=1
return
def updateStatus(self):
if(self.winNum==1):
self.timer.stop()
GPIO1.remove_event_detect(self.gpio_plus)
GPIO1.remove_event_detect(self.gpio_enter)
GPIO1.cleanup()
self.cams = window1.window1()
self.cams.InitUI()
self.close()
self.cams.show()
if(self.winNum==-1):
self.timer.stop()
GPIO1.remove_event_detect(self.gpio_plus)
GPIO1.remove_event_detect(self.gpio_enter)
GPIO1.cleanup()
self.cams = window2.window2()
self.cams.InitUI()
self.close()
self.cams.show()
if __name__ == '__main__':
app=QtWidgets.QApplication(sys.argv)
cams=mainWindow()
cams.show()
sys.exit(app.exec_())
print ("closing the app from main windows")
GPIO1.cleanup()
目前没有回答
相关问题 更多 >
编程相关推荐