简体   繁体   中英

Icon disappears from taskbar when changing window

I am trying to create a CRUD application where it has several windows. List customer, register customer, delete customer, edit customer. For this I am trying to switch from window to another window, the problem is that when I switch the window the icon in the taskbar disappears. I must be doing it wrong. I am used to web applications, this is my first desktop application.

Taskbar icon:

在此处输入图片说明

Pseudo code:

I've tried QStackedWidget but setting pages from the index doesn't look good when you have a lot of pages.

I updated my question using QMainWindow, I did not use QWindow because it does not support setLayout, for example.

import sys
import time
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QMainWindow, QDialog, QApplication, QDesktopWidget, QGridLayout, QLabel, QWidget


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title = 'Main Window'
        self.top = 100
        self.left = 100
        self.width = 350
        self.height = 200
        self.InitUI()
        self.show()

    def InitUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.top, self.left, self.width, self.height)

        qtRectangle = self.frameGeometry()
        centerPoint = QDesktopWidget().availableGeometry().center()
        qtRectangle.moveCenter(centerPoint)
        self.move(qtRectangle.topLeft())

        layout = QGridLayout()

        label1 = QLabel('First Screen - Please wait...')
        label1.setFont(QFont("Times", 25, QFont.Bold))
        layout.addWidget(label1, 0, 0, Qt.AlignCenter)

        central_widget = QWidget()
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)

        QTimer.singleShot(5000, self.goToSecondScreen)

    def goToSecondScreen(self):
        w = SecondWindow(parent=self)
        w.show()
        self.hide()


class SecondWindow(QMainWindow):
    def __init__(self, parent):
        super().__init__(parent=parent)
        self.title = 'Second Window'
        self.top = 100
        self.left = 100
        self.width = 680
        self.height = 500
        self.InitUI()
        self.show()

    def InitUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.top, self.left, self.width, self.height)

        qtRectangle = self.frameGeometry()
        centerPoint = QDesktopWidget().availableGeometry().center()
        qtRectangle.moveCenter(centerPoint)
        self.move(qtRectangle.topLeft())

        layout = QGridLayout()

        label1 = QLabel('Second Screen - Please wait...')
        label1.setFont(QFont("Times", 25, QFont.Bold))
        layout.addWidget(label1, 0, 0, Qt.AlignCenter)

        self.setLayout(layout)

        QTimer.singleShot(5000, self.goToThirdWindow)

    def goToThirdWindow(self):
        w = ThirdWindow(parent=self)
        w.show()
        self.hide()


class ThirdWindow(QMainWindow):
    def __init__(self, parent):
        super().__init__(parent=parent)
        self.title = 'Third Window'
        self.top = 100
        self.left = 100
        self.width = 680
        self.height = 500
        self.InitUI()
        self.show()

    def InitUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.top, self.left, self.width, self.height)

        qtRectangle = self.frameGeometry()
        centerPoint = QDesktopWidget().availableGeometry().center()
        qtRectangle.moveCenter(centerPoint)
        self.move(qtRectangle.topLeft())

        layout = QGridLayout()

        label1 = QLabel('ThirdWindow Screen')
        label1.setFont(QFont("Times", 25, QFont.Bold))
        layout.addWidget(label1, 0, 0, Qt.AlignCenter)

        self.setLayout(layout)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec_())

由于我已经测试了您的代码,我看到无法找到任务栏,因为您正在创建 QDialog 实例,而不是窗口,请尝试创建窗口实例而不是 QDialog 实例

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM