与PyQ一起使用单独的接口文件

2024-07-02 13:48:51 发布

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

我有一个从QtDesigner生成的接口文件,我希望在发生更改时保持不变。你知道吗

有一个名为application.py的主文件来处理所有函数,还有一个文件严格用于接口。你知道吗

我正在使用PyQt5。你知道吗

我还没有找到任何关于这个具体问题的教程,任何指针都会有帮助。你知道吗

代码来自YatsiInterface.py(缩短)

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_YatsiWindow(object):

    def setupUi(self, YatsiWindow):
        YatsiWindow.setObjectName("YatsiWindow")
        YatsiWindow.resize(800, 516)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("Terraria.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        YatsiWindow.setWindowIcon(icon)
        self.windowLayout = QtWidgets.QWidget(YatsiWindow)
        self.windowLayout.setObjectName("windowLayout")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.windowLayout)
        self.quitButton.setObjectName("quitButton")
        self.buttonsLeftLayout.addWidget(self.quitButton)
        YatsiWindow.setCentralWidget(self.windowLayout)
        self.statusbar = QtWidgets.QStatusBar(YatsiWindow)
        self.statusbar.setObjectName("statusbar")
        YatsiWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, YatsiWindow):
        _translate = QtCore.QCoreApplication.translate
        YatsiWindow.setWindowTitle(_translate("YatsiWindow", "Yatsi - Server Interface"))
        self.quitButton.setText(_translate("YatsiWindow", "Quit"))

如何在上述代码中使用self.quitButton.clicked.connect(QtCore.QCoreApplication.instance().quit())?我知道用from YatsiInterface import Ui_YatsiWindow导入文件,但我不知道如何在不编辑接口文件的情况下创建按钮函数。你知道吗

编辑:

我将在下面添加损坏的代码。你知道吗

import sys
from YatsiInterface import Ui_YatsiWindow
from PyQt5 import QtCore, QtGui, QtWidgets

app = QtWidgets.QApplication([])
YatsiWindow = QtWidgets.QMainWindow()
ui = Ui_YatsiWindow()

ui.setupUi(YatsiWindow)
# Here's the bad part
ui.setupUi.btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
# Up there ^

YatsiWindow.show()
sys.exit(app.exec_())

谢谢你的帮助。你知道吗


Tags: 文件fromimportselfuipyqt5translateqtgui
2条回答
from YatsiInterface import Ui_YatsiWindow
from PyQt5 import QtCore, QtGui

class my_application(QtGui.QWidget, Ui_YatsiWindow):

    def __init__(self):
        super(my_application, self).__init__()
        self.setupUi(self)
        self.quitButton.clicked.connect(self.my_mythod)

    def my_method(self):
        pass #all your code for the buttons clicked signal

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    ui = my_application()
    ui.show()
    sys.exit(app.exec_())

这个myu应用程序类正在继承您的用户界面类。现在,您可以编写与按钮相关的函数,而无需编辑用户界面文件。你知道吗

self.quitButton.clicked.connect(self.close) 

这将在单击按钮时关闭用户界面。你知道吗

这就是连接信号的方法

ui.quitButton.clicked.connect(QtCore.QCoreApplication.instance().quit)

话说回来,你可能不想这么做。您可能应该连接到窗口的.close方法。当窗口关闭时,默认情况下,Qt将退出事件循环并退出。你知道吗

ui.quitButton.clicked.connect(YatsiWindow.close)

相关问题 更多 >