在布局之间切换

2024-10-06 12:31:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个简单的屏幕,其中有一个按钮,单击该按钮时会调用另一个屏幕。你知道吗

我找了很多。但我还是做不到: **如何在我的两个布局之间切换,以便在我单击中的按钮时测试.py布局更改为计划**你知道吗

你知道吗测试.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_())

你知道吗计划你知道吗

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_())

Tags: textselfformreturndefsyscontextbutton
1条回答
网友
1楼 · 发布于 2024-10-06 12:31:09

与其使用subprocess.call(),我建议您将scherm设为QDialog。这样,您就可以简单地创建类Ui_PlatjesScherm的实例,而不是在插槽fun(self)中运行另一个脚本。您需要为该类添加一个__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,两个窗口将并行运行。你知道吗

相关问题 更多 >