按钮单击弹出窗口

时间:2012-04-03 20:21:18

标签: pyside

我想创建一个代码,它将显示一个带有按钮的窗口,单击该按钮将创建另一个带有一些字段的窗口(如QLabel,QLineEdit,QSpinBox等)。但是,我不知道如何创建弹出窗口......

这是我的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys                      # Needed for PySide
from PySide.QtCore import *
from PySide.QtGui import *

class Form(QDialog):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        # Create widgets
        self.label1 = QLabel("Label1")
        self.button_open = QPushButton("Open popup")

        self.button = QPushButton("Go!")
        self.qbtn = QPushButton('Quit')

        # Create layout and add widgets
        layout = QVBoxLayout()
        layout.addWidget(self.label1)
        layout.addWidget(self.button_open)

        # Buttons layout
        hbox_buttons = QHBoxLayout()
        hbox_buttons.addStretch(1)
        hbox_buttons.addWidget(self.button)
        hbox_buttons.addWidget(self.qbtn)

        # Main layout
        layout.addStretch(1)
        layout.addWidget(self.button_open)
        layout.addLayout(hbox_buttons)

        self.setLayout(layout)

        # Add buttons slots
        self.button_open.clicked.connect(self.popup)
        self.button.clicked.connect(self.function_runner)
        self.qbtn.clicked.connect(QCoreApplication.instance().quit)


    def popup (self, parent=__init__):
        new_win = # I wonder what should be here

if __name__ == '__main__':
    # Create the Qt Application
    app = QApplication(sys.argv)

    # Create and show the form
    form = Form()
    form.show()

    # Run the main Qt loop
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

我不知道这是不是最好的方式,但是我可以在夜间弄清楚...我希望它会帮助那些遇到类似问题的人。

所以,我(简单地)为第二个窗口创建了一个单独的代码并用

调用它
from subprocess import call
call("./my_2nd_window_code.py")