Automatically convert to py file when ui file is updated in PySide

If you are using QtDesigner to create a .ui file, you will have to convert it to a py file with pyside-uic every time you update the file, which is very annoying.

When the ui file is updated, write a script that automatically converts with pyside-uic.

Use the watchdog module

File update monitoring is a very useful module that uses the watchdog module. Installation is easy with pip.

pip install watchdog

Next, create a file called watchdog_uic.py in the root directory where the ui file exists.

#! usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function, absolute_import
import os
import time
import subprocess
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler

BASE_DIR = os.path.abspath(os.path.dirname(__file__))


class UicHandler(PatternMatchingEventHandler):
    def on_modified(self, event):
        ui_file = event.src_path
        output_file = os.path.splitext(ui_file)[0] + "_ui.py"
        cmd = " ".join([
            "pyside-uic",
            "-o " + output_file,
            ui_file
        ])
        print(cmd)
        subprocess.call(cmd, shell=True)


def main():
    while True:
        event_handler = UicHandler(["*.ui"])
        observer = Observer()
        observer.schedule(event_handler, BASE_DIR, recursive=True)
        observer.start()
        try:
            while True:
                time.sleep(1)
        except KeyboardInterrupt:
            observer.stop()
        observer.join()


if __name__ == "__main__":
    main()

In the above example, the .ui file is changed to the naming convention _ui.py. Please implement the naming convention rules yourself.

python watchdog_uic.py

If you start this script, it will monitor and convert all ui files in the directory under the script file.

I've been using watchdog for automatic conversion for about half a year, but it's too easy to go back to manual conversion anymore.

Postscript 2017-01-06

"The strongest PySide / PyQt development environment is also PyCharm" I wrote a method for automatic conversion on PyCharm. Please refer to that as well.

Recommended Posts

Automatically convert to py file when ui file is updated in PySide
Convert psd file to png in Python
How to specify a .ui file in the dialog / widget GUI in PySide
Convert jupyter to py
Insert type stamp in Sublime Text (automatically updated when saved)
Convert Excel file to text in Python for diff purposes
Convert HTML to text file
Convert genbank file to gff file
Automatically acquire the operation log in the terminal when logging in to Linux
[Python] When you want to use all variables in another file
How to specify a .py file to load at startup in IPython 0.13
How to improve when Spyder's editor is very heavy in Mavericks
What to do when the value type is ambiguous in Python?
Convert markdown to PDF in Python
How to convert 0.5 to 1056964608 in one shot
Run tests automatically when saving file
What to do when the result downloaded via scrapy is in English
Read CSV file with Python and convert it to DataFrame as it is
What to do when the warning "The environment is in consistent ..." appears in the Anaconda environment
How to automatically notify by phone when the python system is down
What to do when a warning message is displayed in pip list