Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update a python ui without rewrite all the button event

Tags:

python

I'm looking for a better way to handle python UI "update" from QtDesigner without overwrite button event. The workflow I got now is:

  1. Design UI layout in QtDesigner
  2. convert .ui to .py by pyuic5
  3. adding button event in .py file
  4. excute .py to see window and button action

So if my UI keep changing the design, how do I keep all the button event I add into .py without being overwrite after convert? Thank you.

like image 530
YHS Avatar asked Sep 14 '26 03:09

YHS


1 Answers

Answer my own question, what I found is have three python files. Main.py, CallUI.py and MainWindow.py. (Named as you want.) So you can keep regenerate UI and override MainWindow.py without clear button event you wrote.

1.Main.py is the one to launch everything, name == "main". Call CAllUI.py's setup function.

#Main.py
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import CallUI


def setUp():
    CallUI.setUpWindow()
    raw_input()


if __name__ == "__main__":
    setUp()

2.CallUI.py is the one to use "QtWidgets.QApplication(sys.argv)" to show UI and add button click functions.

#CallUI.py
import sys
from MainWindow import Ui_Dialog
from PyQt5 import QtCore, QtGui, QtWidgets
import os 

class CallUI(QtWidgets.QWidget):
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.setUpBtnconnect()

    def setUpBtnconnect(self):
        self.ui.pushButton.clicked.connect(self.myFunction)

    def myFunction(self):
        os.system("ipconfig")
        raw_input()

def setUpWindow():
    app = QtWidgets.QApplication(sys.argv)
    nowWindow = CallUI()
    nowWindow.show()
    sys.exit(app.exec_())

3.MainWindow.py is the one you converted from pyuic5, it's describe all the UI layout.

#MainWindow.py
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("MainWindow")
        Dialog.resize(466, 417)
        self.centralwidget = QtWidgets.QWidget(Dialog)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(160, 260, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.menubar = QtWidgets.QMenuBar(Dialog)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 466, 21))
        self.menubar.setObjectName("menubar")
        self.statusbar = QtWidgets.QStatusBar(Dialog)
        self.statusbar.setObjectName("statusbar")

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

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        self.pushButton.setText(_translate("MainWindow", "PushButton"))
like image 54
YHS Avatar answered Sep 16 '26 21:09

YHS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!