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.
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.
"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