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


Messages In This Thread
User Interface to Python - by Raures - Oct-16-2016, 01:21 PM
RE: User Interface to Python - by Yoriz - Oct-16-2016, 02:07 PM
RE: User Interface to Python - by Raures - Oct-16-2016, 02:22 PM
RE: User Interface to Python - by demosthenesk - Oct-16-2016, 06:35 PM
RE: User Interface to Python - by Raures - Oct-16-2016, 07:45 PM
RE: User Interface to Python - by demosthenesk - Oct-16-2016, 08:48 PM
RE: User Interface to Python - by Raures - Oct-16-2016, 10:32 PM
RE: User Interface to Python - by demosthenesk - Oct-17-2016, 04:09 AM
RE: User Interface to Python - by Raures - Oct-27-2016, 12:07 PM
RE: User Interface to Python - by California - Jan-23-2017, 04:23 PM
RE: User Interface to Python - by Raures - Jan-23-2017, 11:58 PM
RE: User Interface to Python - by California - Jan-24-2017, 05:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] User interface for stock portfolio optimisation Fourchelangue 2 2,753 Apr-04-2020, 01:11 PM
Last Post: Fourchelangue
  [Tkinter] User interface for stock portfolio optimisation Fourchelangue 0 2,084 Apr-02-2020, 10:10 PM
Last Post: Fourchelangue
  [PyQt] Invoking user interface design 12203276 1 3,617 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