在布局之间切换

时间:2016-07-11 14:31:25

标签: python pyqt

我创建了一个简单的屏幕,其中的按钮在单击按钮时会调用另一个屏幕。

我搜索了很多。但我仍然无法做到这一点: **如何在我的2个布局之间切换,这样当我单击test.py中的按钮时,布局将更改为scherm.py **

test.py

from PyQt4 import QtCore, QtGui
import subprocess

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig,    _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(400, 300)
        self.Button = QtGui.QPushButton(Form)
        self.Button.setGeometry(QtCore.QRect(50, 40, 261, 231))
        self.Button.setObjectName(_fromUtf8("Button"))
        self.Button.clicked.connect(self.fun)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.Button.setText(_translate("Form", "Button", None))

    def fun(self):
        subprocess.call(" scherm.py 1", shell=True)


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

scherm.py

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig,  _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_PlaatjesScherm(object):
    def setupUi(self, PlaatjesScherm):
        PlaatjesScherm.setObjectName(_fromUtf8("PlaatjesScherm"))
        PlaatjesScherm.resize(654, 528)

        self.retranslateUi(PlaatjesScherm)
        QtCore.QMetaObject.connectSlotsByName(PlaatjesScherm)

    def retranslateUi(self, PlaatjesScherm):
        PlaatjesScherm.setWindowTitle(_translate("PlaatjesScherm", "Plaatjes", None))


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    PlaatjesScherm = QtGui.QWidget()
    ui = Ui_PlaatjesScherm()
    ui.setupUi(PlaatjesScherm)
    PlaatjesScherm.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

我建议您使subprocess.call()成为QDialog,而不是使用scherm。这样,您只需创建类fun(self)的实例,而不是在插槽Ui_PlatjesScherm中运行其他脚本。您需要为该类添加__init__方法。

这就是我打开一个新窗口所做的事情:

class AddStationUI(QObject):
"""
Provides functionality to the UI that allows the user to add a weight to the database.
This UI opens when the user clicks the button "Edit Weights" in the DB Access tab of the Main UI
"""

def __init__(self, db):

    self.db = db
    super(QObject, self).__init__()
    self.window = QtGui.QDialog(None, QtCore.Qt.WindowSystemMenuHint |
                                QtCore.Qt.WindowTitleHint |
                                QtCore.Qt.WindowMinMaxButtonsHint)
    self.window.setSizeGripEnabled(True)

    # Load the UI into self.ui
    self.ui = uic.loadUi('sub_ui/Add_Station.ui', self.window)

    # Set up the event handlers
    self.callback_connector()

    # Execute the ui
    self.window.exec_()

...

然后在我的主要课程中,这是我创建“AddStationUI”窗口的方式:

...
self.ui.myButton.clicked.connect(self.add_station)

def add_station(self):
    AddStationUI(self.db)
...

这将创建AddStationUI的实例,并在__init__方法中加载UI并执行它。这样,您就不必使用subprocess,并且您将有两个并行运行的窗口。