Python Forum
[PyQt] User Interface to Python
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] User Interface to Python
#1
Hi. For all the users that use Qt to make GUI, I've created a simple converter to help them convert the *.ui files to *.py files.
Unfortunately, I was unable to create an executable file, due to an error in cmd when I try to compile with py2exe, which I don't really know how to resolve. You will need pyqt4 to run the script.
Error:
C:\Users\Elder>python "F:\Converter User Interface to Python\Converter User Inte rface to Python.py" py2exe Traceback (most recent call last):   File "F:\Converter User Interface to Python\Converter User Interface to Python .py", line 211, in <module>     ui.setupUi(hGUI)   File "F:\Converter User Interface to Python\Converter User Interface to Python .py", line 139, in setupUi     self.retranslateUi(hGUI)   File "F:\Converter User Interface to Python\Converter User Interface to Python .py", line 148, in retranslateUi     self.ln_destination.setText(_translate("hGUI", self.destination, None)) AttributeError: 'Ui_hGUI' object has no attribute 'destination'
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'C:/Users/Elder/Desktop/untitled.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

__author__ = "Rares Munteanu"
__version__ = "2.0.0"

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
import os, ConfigParser

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_hGUI(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)

        self.browse_engine_file_title = "Browse Engine file"
        self.browse_engine_file_path = os.path.join(os.path.expanduser("~"), "Desktop")
        self.browse_engine_file_extensions = "Batch (*.bat)"
        self.browse_engine_file_found = None

        self.browse_user_interface_file_title = "Browse User Interface file"
        self.browse_user_interface_file_path = os.path.join(os.path.expanduser("~"), "Desktop")
        self.browse_user_interface_file_extensions = "User Interface (*.ui)"
        self.browse_user_interface_file_found = None

        self.browse_destination_title = "Browse Destination"
        self.browse_destination_path = os.path.join(os.path.expanduser("~"), "Desktop")

        self.settings_file = "settings.ini"
        self.engine_file = ""
        self.user_interface_file = ""
        self_destination = ""

        self.config_parser = ConfigParser.RawConfigParser()

        self.file_to_save = None

        self.first_of_all()

    def first_of_all(self):
        if os.path.isfile(self.settings_file): # searches for "cvpsettings.ini", the "ini" extension is necessary!
            self.config_parser.read(self.settings_file) # "ini" extension is necessary!
            self.engine_file = self.config_parser.get("PATHS", "engine")
            self.destination = self.config_parser.get("PATHS", "destination")
        
    def setupUi(self, hGUI):
        hGUI.setObjectName(_fromUtf8("hGUI"))
        hGUI.resize(333, 300)
        hGUI.setMinimumSize(QtCore.QSize(333, 300))
        hGUI.setMaximumSize(QtCore.QSize(333, 300))
        self.label_4 = QtGui.QLabel(hGUI)
        self.label_4.setGeometry(QtCore.QRect(10, 10, 311, 16))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.groupBox_2 = QtGui.QGroupBox(hGUI)
        self.groupBox_2.setGeometry(QtCore.QRect(10, 40, 311, 251))
        self.groupBox_2.setTitle(_fromUtf8(""))
        self.groupBox_2.setObjectName(_fromUtf8("groupBox_2"))
        self.label = QtGui.QLabel(self.groupBox_2)
        self.label.setGeometry(QtCore.QRect(10, 10, 291, 16))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(self.groupBox_2)
        self.label_2.setGeometry(QtCore.QRect(10, 60, 291, 16))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.ln_user_interface_file = QtGui.QLineEdit(self.groupBox_2)
        self.ln_user_interface_file.setGeometry(QtCore.QRect(10, 80, 241, 20))
        self.ln_user_interface_file.setObjectName(_fromUtf8("ln_user_interface_file"))
        self.ln_destination = QtGui.QLineEdit(self.groupBox_2)
        self.ln_destination.setGeometry(QtCore.QRect(10, 130, 241, 20))
        self.ln_destination.setObjectName(_fromUtf8("ln_destination"))
        self.ln_engine_file = QtGui.QLineEdit(self.groupBox_2)
        self.ln_engine_file.setGeometry(QtCore.QRect(10, 30, 241, 20))
        self.ln_engine_file.setObjectName(_fromUtf8("ln_engine_file"))
        self.groupBox = QtGui.QGroupBox(self.groupBox_2)
        self.groupBox.setGeometry(QtCore.QRect(10, 170, 271, 41))
        self.groupBox.setTitle(_fromUtf8(""))
        self.groupBox.setObjectName(_fromUtf8("groupBox"))
        self.label_3 = QtGui.QLabel(self.groupBox)
        self.label_3.setGeometry(QtCore.QRect(10, 10, 211, 21))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.btn_save_settings = QtGui.QPushButton(self.groupBox)
        self.btn_save_settings.setGeometry(QtCore.QRect(220, 10, 41, 23))
        self.btn_save_settings.setText(_fromUtf8(""))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("save.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btn_save_settings.setIcon(icon)
        self.btn_save_settings.setObjectName(_fromUtf8("btn_save_settings"))
        self.btn_browse_engine_file = QtGui.QPushButton(self.groupBox_2)
        self.btn_browse_engine_file.setGeometry(QtCore.QRect(260, 30, 41, 20))
        self.btn_browse_engine_file.setText(_fromUtf8(""))
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8("search.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btn_browse_engine_file.setIcon(icon1)
        self.btn_browse_engine_file.setIconSize(QtCore.QSize(13, 13))
        self.btn_browse_engine_file.setObjectName(_fromUtf8("btn_browse_engine_file"))
        self.btn_browse_user_interface_file = QtGui.QPushButton(self.groupBox_2)
        self.btn_browse_user_interface_file.setGeometry(QtCore.QRect(260, 80, 41, 20))
        self.btn_browse_user_interface_file.setText(_fromUtf8(""))
        self.btn_browse_user_interface_file.setIcon(icon1)
        self.btn_browse_user_interface_file.setIconSize(QtCore.QSize(13, 13))
        self.btn_browse_user_interface_file.setObjectName(_fromUtf8("btn_browse_user_interface_file"))
        self.label_5 = QtGui.QLabel(self.groupBox_2)
        self.label_5.setGeometry(QtCore.QRect(10, 110, 291, 16))
        self.label_5.setObjectName(_fromUtf8("label_5"))
        self.btn_browse_destination = QtGui.QPushButton(self.groupBox_2)
        self.btn_browse_destination.setGeometry(QtCore.QRect(260, 130, 41, 20))
        self.btn_browse_destination.setText(_fromUtf8(""))
        self.btn_browse_destination.setIcon(icon1)
        self.btn_browse_destination.setIconSize(QtCore.QSize(13, 13))
        self.btn_browse_destination.setObjectName(_fromUtf8("btn_browse_destination"))
        self.btn_convert = QtGui.QPushButton(self.groupBox_2)
        self.btn_convert.setGeometry(QtCore.QRect(20, 220, 101, 23))
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap(_fromUtf8("correct.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btn_convert.setIcon(icon2)
        self.btn_convert.setObjectName(_fromUtf8("btn_convert"))
        self.lbl_status = QtGui.QLabel(self.groupBox_2)
        self.lbl_status.setGeometry(QtCore.QRect(130, 223, 171, 20))
        self.lbl_status.setAlignment(QtCore.Qt.AlignCenter)
        self.lbl_status.setObjectName(_fromUtf8("lbl_status"))

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

    def retranslateUi(self, hGUI):
        hGUI.setWindowTitle(_translate("hGUI", "Converter: User Interface to Python", None))
        self.label_4.setText(_translate("hGUI", "<html><head/><body><p>A simple <span style=\" font-weight:600;\">User Interface</span> (<span style=\" font-weight:600;\">*.ui</span>) to <span style=\" font-weight:600;\">Python</span> (<span style=\" font-weight:600;\">*.py</span>) converter.</p></body></html>", None))
        self.label.setText(_translate("hGUI", "<html><head/><body><p>Engine file (<span style=\" font-weight:600;\">*.bat</span>) - the pyuic4.bat file</p></body></html>", None))
        self.label_2.setText(_translate("hGUI", "<html><head/><body><p>User Interface file (<span style=\" font-weight:600;\">*.ui</span>) - the file you want to convert</p></body></html>", None))
        self.ln_user_interface_file.setText(_translate("hGUI", self.user_interface_file, None))
        self.ln_destination.setText(_translate("hGUI", self.destination, None))
        self.ln_engine_file.setText(_translate("hGUI", self.engine_file, None))
        self.label_3.setText(_translate("hGUI", "<html><head/><body><p>Save <span style=\" font-weight:600;\">Engine file</span> and <span style=\" font-weight:600;\">Destination</span> paths</p></body></html>", None))
        self.btn_save_settings.setToolTip(_translate("hGUI", "Save <b>Engine</b> and <b>Destination</b> path to <b>" + self.settings_file + "</b>", None))
        self.btn_browse_engine_file.setToolTip(_translate("hGUI", "Browse <b>Engine</b> file", None))
        self.btn_browse_user_interface_file.setToolTip(_translate("hGUI", "Browse <b>User Interface</b> file", None))
        self.label_5.setText(_translate("hGUI", "Destination - where to save the converted file", None))
        self.btn_browse_destination.setToolTip(_translate("hGUI", "Browse <b>Destination</b>", None))
        self.btn_convert.setToolTip(_translate("hGUI", "Convert <b>User Interface</b> file", None))
        self.btn_convert.setText(_translate("hGUI", "Convert", None))
        self.lbl_status.setText(_translate("hGUI", "Click on \"<b>Convert</b>\" to... convert.", None))

        self.btn_browse_engine_file.clicked.connect(self.browse_engine_file)
        self.btn_browse_user_interface_file.clicked.connect(self.browse_user_interface_file)
        self.btn_browse_destination.clicked.connect(self.browse_destination)

        self.btn_save_settings.clicked.connect(self.save_settings)

        self.btn_convert.clicked.connect(self.convert)

    def browse_engine_file(self):
        self.browse_engine_file_found = QFileDialog.getOpenFileName(hGUI, self.browse_engine_file_title, self.browse_engine_file_path, self.browse_engine_file_extensions)
        if self.browse_engine_file_found:
            self.ln_engine_file.setText(self.browse_engine_file_found)

    def browse_user_interface_file(self):
        self.browse_user_interface_file_found = QFileDialog.getOpenFileName(hGUI, self.browse_user_interface_file_title, self.browse_user_interface_file_path, self.browse_user_interface_file_extensions)
        if self.browse_user_interface_file_found:
            self.ln_user_interface_file.setText(self.browse_user_interface_file_found)

    def browse_destination(self):
        self.browse_destination_found = QFileDialog.getExistingDirectory(hGUI, self.browse_destination_title, self.browse_destination_path)
        if self.browse_destination_found:
            self.ln_destination.setText(self.browse_destination_found)

    def save_settings(self):
        self.file_to_save = open(self.settings_file, "w")
        
        self.engine_file = self.ln_engine_file.text()
        self.destination = self.ln_destination.text()

        self.config_parser.set("PATHS", "engine", self.engine_file)
        self.config_parser.set("PATHS", "destination", self.destination)

        self.config_parser.write(self.file_to_save)

        self.file_to_save.close()

        self.lbl_status.setText("Settings saved.")

    def convert(self):
        if str(self.ln_engine_file.text()) == " " or str(self.ln_user_interface_file.text()) == " " or str(self.ln_destination.text()) == " ":
            os.system(str(self.ln_engine_file.text()) + ' -o "' + str(self.ln_destination.text()) + "\\convertedfile.py"'" "' + str(self.ln_user_interface_file.text()) + '"')                    

            self.lbl_status.setText("Converted to <b>convertedfile.py</b>.")
        else:
            self.lbl_status.setText("Path missing.")

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    hGUI = QtGui.QWidget()
    ui = Ui_hGUI()
    ui.setupUi(hGUI)
    hGUI.show()
    sys.exit(app.exec_())
I've provided a link where you can download an archive containing the source code, *.png pictures necessary for buttons and a *.ini file that will contain some paths set by you (can't attach files).
Opinions and feedback are welcome.

Download link to Uploadfiles.io
Reply
#2
In the __init__ of the class Ui_hGUI, when the destination attribute is assigned it has an underscore
self_destination = ""
instead of of a dot
self.destination = ""
at the end of the __ini__ the method first_of_all is called which also sets self.destination but only when the
if os.path.isfile(self.settings_file): 
is true, which might be false as an exe because it cant find the file (as an exe usually full file paths are better)
Reply
#3
Thank you for yoyr reply! I will fix it and provide the executable file as soon as possible.
Reply
#4
In Windows PyQt version is there the pyuic4 utility?
pyuic4 converts the myGUI.ui to myGUI.py  ;)
Reply
#5
The pyuic4.bat file is in Python27\Lib\site-packages\PyQt4\, this script was made in Windows, it's not tested on other platforms.
Reply
#6
(Oct-16-2016, 07:45 PM)Raures Wrote: The pyuic4.bat file is in Python27\Lib\site-packages\PyQt4\, this script was made in Windows, it's not tested on other platforms.

Yes i saw it.

You can use the pyuic4.bat to convert your *.ui files to *.py.
It is the official way to do this with pyuic4.bat

This bat file is provided by PyQt for conversion ui files to py.
Reply
#7
That is what my software is using. You must pick the pyuic4.bat file as 'engine file', then the *.ui file to convert, then the destination, you can save pyuic4 and destination paths so in the future you will only have to browse the file you want to convert.
The thing is, you won't have to write all the stuff in command prompt to convert the file.
Reply
#8
ok, so it is a frontend for pyuic4. Nice!
Reply
#9
I've worked on the program a little and managed to add some features:
  • You can choose the name of the converted file
  • If a file with the same name exists, you can "Overwrite" or "Rename", the "Rename" feature will rename the file, adding a random number from 1 to 100 in its name
  • The save button will save the *.bat file path, destination path, the desired name for the converted file, and one of the two options, "Overwrite" or "Rename"
Here's the code:
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'G:/untitled.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

__author__ = "Rares Munteanu"
__version__ = "2.1.1"

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
import os, ConfigParser, random

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_hGUI(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)

        self.window_title = "User Interface to Python"

        self.browse_engine_file_title = "Browse Engine file"
        self.browse_engine_file_path = os.path.join(os.path.expanduser("~"), "Desktop")
        self.browse_engine_file_extensions = "Batch (*.bat)"
        self.browse_engine_file_found = None

        self.browse_user_interface_file_title = "Browse User Interface file"
        self.browse_user_interface_file_path = os.path.join(os.path.expanduser("~"), "Desktop")
        self.browse_user_interface_file_extensions = "User Interface (*.ui)"
        self.browse_user_interface_file_found = None

        self.browse_destination_title = "Browse Destination"
        self.browse_destination_path = os.path.join(os.path.expanduser("~"), "Desktop")

        self.settings_file = "settings.ini"
        self.engine_file = ""
        self.user_interface_file = ""
        self.destination = ""
        self.save_as = ""
        self.overwrite = "1"

        self.main_icon = "icon.ico"
        self.search_image = "search.png"
        self.correct_image = "correct.png"
        self.save_settings_image = "save.png"

        self.all_files = []
        
        self.config_parser = ConfigParser.RawConfigParser()

        self.file_to_save = None
        self.file_to_compile = None
        
        self.first_of_all()
        
    def first_of_all(self):
        if os.path.isfile(self.settings_file): # searches for "cvpsettings.ini", the "ini" extension is necessary!
            self.config_parser.read(self.settings_file) # "ini" extension is necessary!
            self.engine_file = self.config_parser.get("PATHS", "engine")
            self.destination = self.config_parser.get("PATHS", "destination")
            self.save_as = self.config_parser.get("PATHS", "saveas")
            self.overwrite = self.config_parser.get("PATHS", "overwrite")
            
    def setupUi(self, hGUI):
        hGUI.setObjectName(_fromUtf8("hGUI"))
        hGUI.resize(331, 372)
        hGUI.setMinimumSize(QtCore.QSize(331, 372))
        hGUI.setMaximumSize(QtCore.QSize(331, 372))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8(self.main_icon)), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        hGUI.setWindowIcon(icon)
        self.label_4 = QtGui.QLabel(hGUI)
        self.label_4.setGeometry(QtCore.QRect(10, 10, 311, 16))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.groupBox_2 = QtGui.QGroupBox(hGUI)
        self.groupBox_2.setGeometry(QtCore.QRect(10, 40, 311, 321))
        self.groupBox_2.setTitle(_fromUtf8(""))
        self.groupBox_2.setObjectName(_fromUtf8("groupBox_2"))
        self.label = QtGui.QLabel(self.groupBox_2)
        self.label.setGeometry(QtCore.QRect(10, 10, 291, 16))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(self.groupBox_2)
        self.label_2.setGeometry(QtCore.QRect(10, 60, 291, 16))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.ln_user_interface_file = QtGui.QLineEdit(self.groupBox_2)
        self.ln_user_interface_file.setGeometry(QtCore.QRect(10, 80, 241, 20))
        self.ln_user_interface_file.setObjectName(_fromUtf8("ln_user_interface_file"))
        self.ln_destination = QtGui.QLineEdit(self.groupBox_2)
        self.ln_destination.setGeometry(QtCore.QRect(10, 130, 241, 20))
        self.ln_destination.setObjectName(_fromUtf8("ln_destination"))
        self.ln_engine_file = QtGui.QLineEdit(self.groupBox_2)
        self.ln_engine_file.setGeometry(QtCore.QRect(10, 30, 241, 20))
        self.ln_engine_file.setObjectName(_fromUtf8("ln_engine_file"))
        self.groupBox = QtGui.QGroupBox(self.groupBox_2)
        self.groupBox.setGeometry(QtCore.QRect(10, 240, 271, 41))
        self.groupBox.setTitle(_fromUtf8(""))
        self.groupBox.setObjectName(_fromUtf8("groupBox"))
        self.label_3 = QtGui.QLabel(self.groupBox)
        self.label_3.setGeometry(QtCore.QRect(10, 10, 211, 21))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.btn_save_settings = QtGui.QPushButton(self.groupBox)
        self.btn_save_settings.setGeometry(QtCore.QRect(220, 10, 41, 23))
        self.btn_save_settings.setText(_fromUtf8(""))
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8(self.save_settings_image)), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btn_save_settings.setIcon(icon1)
        self.btn_save_settings.setObjectName(_fromUtf8("btn_save_settings"))
        self.btn_browse_engine_file = QtGui.QPushButton(self.groupBox_2)
        self.btn_browse_engine_file.setGeometry(QtCore.QRect(260, 30, 41, 20))
        self.btn_browse_engine_file.setText(_fromUtf8(""))
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap(_fromUtf8(self.search_image)), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btn_browse_engine_file.setIcon(icon2)
        self.btn_browse_engine_file.setIconSize(QtCore.QSize(13, 13))
        self.btn_browse_engine_file.setObjectName(_fromUtf8("btn_browse_engine_file"))
        self.btn_browse_user_interface_file = QtGui.QPushButton(self.groupBox_2)
        self.btn_browse_user_interface_file.setGeometry(QtCore.QRect(260, 80, 41, 20))
        self.btn_browse_user_interface_file.setText(_fromUtf8(""))
        self.btn_browse_user_interface_file.setIcon(icon2)
        self.btn_browse_user_interface_file.setIconSize(QtCore.QSize(13, 13))
        self.btn_browse_user_interface_file.setObjectName(_fromUtf8("btn_browse_user_interface_file"))
        self.label_5 = QtGui.QLabel(self.groupBox_2)
        self.label_5.setGeometry(QtCore.QRect(10, 110, 291, 16))
        self.label_5.setObjectName(_fromUtf8("label_5"))
        self.btn_browse_destination = QtGui.QPushButton(self.groupBox_2)
        self.btn_browse_destination.setGeometry(QtCore.QRect(260, 130, 41, 20))
        self.btn_browse_destination.setText(_fromUtf8(""))
        self.btn_browse_destination.setIcon(icon2)
        self.btn_browse_destination.setIconSize(QtCore.QSize(13, 13))
        self.btn_browse_destination.setObjectName(_fromUtf8("btn_browse_destination"))
        self.btn_convert = QtGui.QPushButton(self.groupBox_2)
        self.btn_convert.setGeometry(QtCore.QRect(10, 290, 101, 23))
        icon3 = QtGui.QIcon()
        icon3.addPixmap(QtGui.QPixmap(_fromUtf8(self.correct_image)), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.btn_convert.setIcon(icon3)
        self.btn_convert.setObjectName(_fromUtf8("btn_convert"))
        self.lbl_status = QtGui.QLabel(self.groupBox_2)
        self.lbl_status.setGeometry(QtCore.QRect(120, 290, 181, 21))
        self.lbl_status.setAlignment(QtCore.Qt.AlignCenter)
        self.lbl_status.setObjectName(_fromUtf8("lbl_status"))
        self.label_6 = QtGui.QLabel(self.groupBox_2)
        self.label_6.setGeometry(QtCore.QRect(10, 160, 291, 16))
        self.label_6.setObjectName(_fromUtf8("label_6"))
        self.ln_save_as = QtGui.QLineEdit(self.groupBox_2)
        self.ln_save_as.setGeometry(QtCore.QRect(10, 180, 121, 20))
        self.ln_save_as.setObjectName(_fromUtf8("ln_save_as"))
        self.label_7 = QtGui.QLabel(self.groupBox_2)
        self.label_7.setGeometry(QtCore.QRect(140, 180, 161, 16))
        self.label_7.setObjectName(_fromUtf8("label_7"))
        self.label_8 = QtGui.QLabel(self.groupBox_2)
        self.label_8.setGeometry(QtCore.QRect(10, 210, 131, 16))
        self.label_8.setObjectName(_fromUtf8("label_8"))
        self.rdo_overwrite = QtGui.QRadioButton(self.groupBox_2)
        self.rdo_overwrite.setGeometry(QtCore.QRect(150, 210, 71, 17))
        self.rdo_overwrite.setObjectName(_fromUtf8("rdo_overwrite"))
        self.rdo_rename = QtGui.QRadioButton(self.groupBox_2)
        self.rdo_rename.setGeometry(QtCore.QRect(230, 210, 71, 17))
        self.rdo_rename.setObjectName(_fromUtf8("rdo_rename"))

        if self.overwrite == "1":
            self.rdo_overwrite.setChecked(True)
        else:
            self.rdo_rename.setChecked(True)

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

    def retranslateUi(self, hGUI):
        hGUI.setWindowTitle(_translate("hGUI", "User Interface to Python", None))
        self.label_4.setText(_translate("hGUI", "<html><head/><body><p>A simple <span style=\" font-weight:600;\">User Interface</span> (<span style=\" font-weight:600;\">*.ui</span>) to <span style=\" font-weight:600;\">Python</span> (<span style=\" font-weight:600;\">*.py</span>) converter.</p></body></html>", None))
        self.label.setText(_translate("hGUI", "<html><head/><body><p>Engine file (<span style=\" font-weight:600;\">*.bat</span>) - the pyuic4.bat file</p></body></html>", None))
        self.label_2.setText(_translate("hGUI", "<html><head/><body><p>User Interface file (<span style=\" font-weight:600;\">*.ui</span>) - the file you want to convert</p></body></html>", None))
        self.ln_user_interface_file.setText(_translate("hGUI", self.user_interface_file, None))
        self.ln_destination.setText(_translate("hGUI", self.destination, None))
        self.ln_engine_file.setText(_translate("hGUI", self.engine_file, None))
        self.label_3.setText(_translate("hGUI", "<html><head/><body><p>Save <span style=\" font-weight:600;\">Engine file</span> and <span style=\" font-weight:600;\">Destination</span> paths</p></body></html>", None))
        self.btn_save_settings.setToolTip(_translate("hGUI", "Save current paths to <b>" + self.settings_file + "</b>", None))
        self.btn_browse_engine_file.setToolTip(_translate("hGUI", "Browse <b>Engine</b> file", None))
        self.btn_browse_user_interface_file.setToolTip(_translate("hGUI", "Browse <b>User Interface</b> file", None))
        self.label_5.setText(_translate("hGUI", "Destination - where to save the converted file", None))
        self.btn_browse_destination.setToolTip(_translate("hGUI", "Browse <b>Destination</b>", None))
        self.btn_convert.setToolTip(_translate("hGUI", "Convert <b>User Interface</b> file", None))
        self.btn_convert.setText(_translate("hGUI", "Convert", None))
        self.lbl_status.setText(_translate("hGUI", "Click on \"<b>Convert</b>\" to... convert.", None))
        self.label_6.setText(_translate("hGUI", "Save converted file as...", None))
        self.ln_save_as.setText(_translate("hGUI", self.save_as, None))
        self.label_7.setText(_translate("hGUI", "<html><head/><body><p>Extension will be forced to <span style=\" font-weight:600;\">*.py</span></p></body></html>", None))
        self.label_8.setText(_translate("hGUI", "If the file already exists...", None))
        self.rdo_overwrite.setToolTip(_translate("hGUI", "Overwrite the <b>existing</b> file", None))
        self.rdo_overwrite.setText(_translate("hGUI", "Overwrite", None))
        self.rdo_rename.setToolTip(_translate("hGUI", "Rename the <b>converted</b> file", None))
        self.rdo_rename.setText(_translate("hGUI", "Rename", None))

        self.btn_browse_engine_file.clicked.connect(self.browse_engine_file)
        self.btn_browse_user_interface_file.clicked.connect(self.browse_user_interface_file)
        self.btn_browse_destination.clicked.connect(self.browse_destination)

        self.btn_save_settings.clicked.connect(self.save_settings)

        self.btn_convert.clicked.connect(self.convert)

    def browse_engine_file(self):
        self.browse_engine_file_found = QFileDialog.getOpenFileName(hGUI, self.browse_engine_file_title, self.browse_engine_file_path, self.browse_engine_file_extensions)
        if self.browse_engine_file_found:
            self.ln_engine_file.setText(self.browse_engine_file_found)

    def browse_user_interface_file(self):
        self.browse_user_interface_file_found = QFileDialog.getOpenFileName(hGUI, self.browse_user_interface_file_title, self.browse_user_interface_file_path, self.browse_user_interface_file_extensions)
        if self.browse_user_interface_file_found:
            self.ln_user_interface_file.setText(self.browse_user_interface_file_found)
            self.ln_save_as.setText(os.path.splitext(os.path.basename(str(self.browse_user_interface_file_found)))[0])

    def browse_destination(self):
        self.browse_destination_found = QFileDialog.getExistingDirectory(hGUI, self.browse_destination_title, self.browse_destination_path)
        if self.browse_destination_found:
            self.ln_destination.setText(self.browse_destination_found)

    def save_settings(self):
        self.file_to_save = open(self.settings_file, "w")
        
        self.engine_file = self.ln_engine_file.text()
        self.destination = self.ln_destination.text()
        self.save_as = self.ln_save_as.text()

        self.config_parser.set("PATHS", "engine", self.engine_file)
        self.config_parser.set("PATHS", "destination", self.destination)
        self.config_parser.set("PATHS", "saveas", self.save_as)

        if self.rdo_overwrite.isChecked():
            self.config_parser.set("PATHS", "overwrite", "1")
        else:
            self.config_parser.set("PATHS", "overwrite", "0")

        self.config_parser.write(self.file_to_save)

        self.file_to_save.close()

        self.lbl_status.setText("Settings saved.")

    def convert(self):
        if not self.ln_engine_file.text().isEmpty() or not self.ln_user_interface_file.text().isEmpty() or not self.ln_destination.text().isEmpty():
            self.file_to_compile = str(self.ln_save_as.text())
            if self.rdo_overwrite.isChecked():
                os.system(str(self.ln_engine_file.text()) + ' -o "' + str(self.ln_destination.text()) + "\\" + self.file_to_compile + ".py"'" "' + str(self.ln_user_interface_file.text()) + '"')
            else:
                del self.all_files[:]
                self.all_files = [f for f in os.listdir(str(self.ln_destination.text()))]
                print self.all_files
                for py_file in self.all_files:
                    if os.path.splitext(os.path.basename(py_file))[0] == self.file_to_compile:
                        print "Passed"
                        self.file_to_compile += str(random.randint(1, 100))
                        
                os.system(str(self.ln_engine_file.text()) + ' -o "' + str(self.ln_destination.text()) + "\\" + self.file_to_compile + ".py"'" "' + str(self.ln_user_interface_file.text()) + '"')
                
            self.lbl_status.setText("Converted to <b>" + str(self.ln_save_as.text()) + ".py</b>.")
            
        else:
            self.lbl_status.setText("Path missing.")
        
if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    hGUI = QtGui.QWidget()
    ui = Ui_hGUI()
    ui.setupUi(hGUI)
    hGUI.show()
    sys.exit(app.exec_())
Executable and source code download links below.
Reply
#10
Hello Raures, would you be so kind to post download link once again. The last one doesn't work anymore.

Vlado
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] User interface for stock portfolio optimisation Fourchelangue 2 2,682 Apr-04-2020, 01:11 PM
Last Post: Fourchelangue
  [Tkinter] User interface for stock portfolio optimisation Fourchelangue 0 2,038 Apr-02-2020, 10:10 PM
Last Post: Fourchelangue
  [PyQt] Invoking user interface design 12203276 1 3,481 May-22-2017, 06:46 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020