pyqt implementation.ui file batch conversion to corresponding.py file script

  • 2021-06-29 11:19:07
  • OfStack

As you all know, you need to manually convert the.ui file to.py after creating the interface.ui file with the designer tool of Pyqt4 before you can write an event. It can be cumbersome to create many.ui files one at a time, and the doctrine that lazy programmers are good programmers is true.The blogger wrote a simple script that automatically identifies the untranslated.ui files in a given directory and converts them to.py files, directly to the code:


#!/usr/bin/env python
#coding=utf-8
'''
 Automatically identify non-converted UI File and convert to corresponding version of py file 
     -  by Joy
'''
import os
 
#  Here's the path by UI Path to file storage 
path = r'D:\SVNzhangy\fast-transfer\src'
 
#  Number of files converted successfully 
count = 0
for root, dirs, files in os.walk(path):
  filename = []
  for file in files:
    if file.endswith('.py'):
      filename2 = file.split('.')[0]
      filename.append(filename2)
 
  version = input(" Please enter 1 or 2  confirm pyqt Convert Version (1 - pyqt4,2 - pyqt5):")
 
  for file in files:
    if file.endswith('.ui'):
      filename1 = file.split('.')[0]
      filename4 = filename1 + "_qt4"
      filename5 = filename1 + "_qt5"
      try:
        if "1" == str(version):
          if filename.count(filename4) == 0:
            os.system('pyuic4 -o %s.py %s.ui -d'%(path + '\\' + filename4, path+'\\'+filename1))
            count+=1
        if "2" == str(version):
          if filename.count(filename5) == 0:
            os.system('pyuic5 -o %s.py %s.ui -d' % (path + '\\' + filename5, path + '\\' + filename1))
            count += 1
      except Exception as e:
        print " File conversion error, check if the installation instructions correspond correctly PyQt"
        raise e
 
  if count == 0:
    print " Not yet needed to convert UI file "
  else:
    print ' Conversion complete, please check!This Co-conversion  ' + str(count) + '  Files '
 

Happy Knock!

Updated on August 30, 2017

The blogger made an error when compiling the UI file. Adding -d after the command could print the error message. It was found that the Chinese font was used in the UI file. Changing the font to English is ok. Record 1, the code has been modified.

At the same time, because pyqt personal applications are free, but will be charged for commercial use, so for convenience, it is recommended to use the pip management tools to download the pyside library to write applications. There are many tutorials in Baidu that are not described here. There is little difference between the usage and pyqt. The command to compile UI files is replaced by pyside-uic. Note!Either pyuic4 or pysi-uic, the system environment variables for both exe files need to be set, otherwise the command cannot be found.

Updated in 2008/2/22

The code adds a choice for a converted version of pyqt5, but make sure the corresponding version is installed before converting.


Related articles: