Pyqt implements batch conversion of. UI file to corresponding. Py file script


As we all know, after creating the interface. UI file through PyQt4 designer tool, you need to manually CMD the command to convert the. UI file to. Py before you can write the event. If you encounter many. UI files to be converted one by one, it will be very difficult. Based on the belief that lazy programmers are good programmers, the blogger wrote a UI file that can automatically identify the unconverted under a given directory. And convert it into a simple script of. Py file, directly on the code page:

#!/usr/bin/env python
Automatically recognize the unconverted UI file and convert it to the corresponding version of Py file
    —— by Joy
import os
#The path here is the path where the UI file is stored
path = r'D:\SVNzhangy\fast-transfer\src'
#Record the number of files successfully converted
count = 0
for root, dirs, files in os.walk(path):
  filename = []
  for file in files:
    if file.endswith('.py'):
      filename2 = file.split('.')[0]
  Version = input ("please enter 1 or 2 to confirm the pyqt conversion Version (1 - pyqt4,2 - pyqt5):")
  for file in files:
    if file.endswith('.ui'):
      filename1 = file.split('.')[0]
      filename4 = filename1 + "_qt4"
      filename5 = filename1 + "_qt5"
        if "1" == str(version):
          if filename.count(filename4) == 0:
            os.system('pyuic4 -o %s.ui -d'%(path + '\' + filename4, path+'\'+filename1))
        if "2" == str(version):
          if filename.count(filename5) == 0:
            os.system('pyuic5 -o %s.ui -d' % (path + '\' + filename5, path + '\' + filename1))
            count += 1
      except Exception as e:
        Print "file conversion error, please check if pyqt corresponding to installation instruction is correct"
        raise e
  if count == 0:
    Print "no UI file to convert"
    Print 'conversion completed, please check! A total of '+ str (count) +' files' are converted this time

Have a good time~

Updated on August 30, 2017

When compiling the UI file, the blogger made an error. Add – d after the command to print the error information. It is found that the Chinese font is used in the UI file, so it is OK to change it to English font. Record that the code has been modified.

At the same time, because pyqt personal application is free, but it will be charged for commercial use, so for convenience, it is recommended to use PIP management tool to download pyside library to write applications. Baidu has a lot of tutorials that are not covered here. There is almost no difference between usage and pyqt. The command to compile UI file can be replaced by pyside UIC. Note! Whether pyuic4 or pysi UIC, the system environment variables of these two exe files need to be set, otherwise the command will not be found.

Updated on February 22, 2018

The code adds the choice of pyqt5, but please make sure that the corresponding version is installed before conversion.

The above pyqt implementation. UI file batch conversion to the corresponding. Py file script is all the content shared by the editor. I hope it can give you a reference, and I hope you can support developpaer more.