简体   繁体   English

开二 window PyQt

[英]Opening a second window PyQt

I have seen other examples of this but they use我见过这方面的其他例子,但他们使用

class secondWindow():
    def __init__(self):
        super().__init__()
        #some code

class Window():
    def __init__(self):
        super().__init__()
        # some code

def main():
    app = QtGui.QApplication(sys.argv)
    main = Window()
    main.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()  

however my code looks like this但是我的代码看起来像这样

class secondWindow():
    def __init__(self):
        some code

class overviewWindow():
    def __init__(self):
        # Database setup
        conn = None
        try:
            conn = sqlite3.connect("exercises.db")
            print("[*] Connected successfully to the database")
        except: 
            print("[*] Error connecting to the databse")

        # Window setup
        self.app = QApplication(sys.argv)
        self.w = QWidget()
        self.w.resize(winWidth, winHeight)
        self.w.move(200, 20)
        self.w.setWindowTitle("Test")
        self.w.setStyleSheet("background-color: #F4F0BB")
        self.w.show()
        
        self.showUI()

        sys.exit(self.app.exec_())


    def showUI(self):
        # Title
        self.title = QLabel() 
        self.title.setParent(self.w)
        self.title.setText("Overblik")
        self.title.resize(winWidth, 100)
        self.title.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.title.setStyleSheet("font-size: 45pt")
        self.title.show()

        self.frame = QFrame()
        self.frame.resize(winWidth, 900)
        self.frame.move(0, 100)
        self.frame.setParent(self.w)
        self.frame.show()

        # Buttons
        xBuffer = 130
        yBuffer = 130
        sum = 0
        for i in range(0, 6):
            for j in range(0, 4):
                sum += 1
                self.button2 = Button(self.frame, 75+(j*xBuffer), 75+(i*yBuffer), str(sum))

if __name__ == "__main__":
    overviewWindow()

My problem is i want to open a second window with the same window style (same background and size) as the first but with different content ie different labels and buttons我的问题是我想打开第二个 window 与第一个具有相同的 window 样式(相同的背景和大小)但具有不同的内容,即不同的标签和按钮

I have tried modifying my code so it looks like the other examples but i cant figure out how to rewrite it without rewriting my entire file.我曾尝试修改我的代码,使其看起来像其他示例,但我无法弄清楚如何在不重写整个文件的情况下重写它。

Try having all your styles and customization in a stylesheet that way you can apply the style to all classes you need to without individually changing elements here is an example:尝试将所有 styles 和自定义设置在样式表中,这样您就可以将样式应用于所有需要的类,而无需单独更改元素,这里是一个示例:

from PySide2 import QtCore
from PySide2.QtWidgets import *
from PySide2.QtGui import *


class Widget2(QWidget):
    def __init__(self, parent=None):
        super(Widget2, self).__init__(parent)
        self.gui()

    def gui(self):
        self.w1 = self
        self.w1.setAutoFillBackground(True)
        self.w1.setWindowTitle("")
        self.w1.resize(500, 450)
        self.w1.setCursor(Qt.ArrowCursor)
        self.w1.setToolTip("")
        self.check1 = QCheckBox("Check Box", self.w1)
        self.check1.setChecked(0)
        self.check1.move(180, 220)
        self.check1.resize(90, 22)
        self.check1.setCursor(Qt.ArrowCursor)
        self.check1.setToolTip("")
        self.button2 = QToolButton(self.w1)
        self.button2.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
        self.button2.setText("Button")
        self.button2.move(40, 110)
        self.button2.resize(90, 22)
        self.button2.setCursor(Qt.ArrowCursor)
        self.button2.setToolTip("")
        return self.w1


    
    
class Widget1(QWidget):
    def __init__(self, parent=None):
        super(Widget1, self).__init__(parent)
        self.gui()

    def gui(self):
        self.w1 = self
        self.w1.setAutoFillBackground(True)
        self.w1.setWindowTitle("")
        self.w1.resize(500, 450)
        self.w1.setCursor(Qt.ArrowCursor)
        self.w1.setToolTip("")
        self.button1 = QToolButton(self.w1)
        self.button1.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
        self.button1.setText("Button")
        self.button1.move(40, 110)
        self.button1.resize(90, 22)
        self.button1.setCursor(Qt.ArrowCursor)
        self.button1.setToolTip("")
        self.button1.clicked.connect(self.event)
        return self.w1

    def event(self):
        b = Widget2()
        b.setStyleSheet("""
QToolButton
{
color: yellow;
background-color: red;
}""")
        b.show()

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    a = Widget1()
    a.setStyleSheet("""
QToolButton
{
color: yellow;
background-color: red;
}""")
    a.show()
    sys.exit(app.exec_())

As you can see I have one stylesheet that is applied to both of my QWidgets, ideally you would apply the stylesheet to QApplication but I could not reproduce an example of it working.如您所见,我有一个应用于我的两个 QWidget 的样式表,理想情况下您可以将该样式表应用于 QApplication,但我无法重现它的工作示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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