簡體   English   中英

更改窗口時圖標從任務欄中消失

[英]Icon disappears from taskbar when changing window

我正在嘗試創建一個具有多個窗口的 CRUD 應用程序。 列出客戶、注冊客戶、刪除客戶、編輯客戶。 為此,我試圖從窗口切換到另一個窗口,問題是當我切換窗口時,任務欄中的圖標消失了。 我一定是做錯了。 我習慣了 Web 應用程序,這是我的第一個桌面應用程序。

任務欄圖標:

在此處輸入圖片說明

偽代碼:

我試過 QStackedWidget 但是當你有很多頁面時,從索引設置頁面看起來不太好。

我使用 QMainWindow 更新了我的問題,我沒有使用 QWindow,因為它不支持 setLayout,例如。

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 實例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM