foreword
I originally wanted to use Java as the client, but later found that many algorithms are still more convenient to have ready-made in Python.
So I finally chose pyside6. But after designing with Designer (QT Designer), the Chinese part of the generated code is displayed as unicode, which can also be understood as garbled characters.
like this:self.pushButton.setText(QCoreApplication.translate("dialog", u"\u767b\u5f55", None))
Although it does not affect the final presentation, but as an obsessive-compulsive disorder, I must solve it! !
Here is the workaround:
1. Download and install ascii2uni
Mac users can use homebrew to install: brew install uni2ascii
(I am a MacOS system)
Other users can go to the official website to download and install:https://billposer.org/Software/uni2ascii.html#downloads
After installation, use the following similar commands to generate a normal Chinese display:
/Library/Frameworks/Python.framework/Versions/3.8/bin/pyside6-uic LoginWindow.ui | ascii2uni -a U > ui_LoginWindow.py
/Library/Frameworks/Python.framework/Versions/3.8/bin/pyside6-uic
is your uic addressLoginWindow.ui
Is the address of your design UI fileui_LoginWindow.py
is the output address of the py file
The above three are customized content, and the others are fixed commands.
but! How troublesome it is to enter commands every time! The following is the configuration in PyCharm
2. PyCharm configuration
We can add external tools to PyCharm so that we can generate Python code every time.
There is a big hole here, let me tell you first.
Originally, adding an external tool to configure the command in PyCharm should have the same effect as the command line execution.
Program: /Library/Frameworks/Python.framework/Versions/3.8/bin/pyside6-uic
Arguments: $FileName$ | ascii2uni -a U > UI_$FileNameWithoutExtension$.py
Working Directory: $FileDir$
But! but!!!
I don't know what happened to PyCharm, the command he finally executed was:/Library/Frameworks/Python.framework/Versions/3.8/bin/pyside6-uic LoginWindow.ui "|" ascii2uni -a U > ui_LoginWindow.py
| is wrapped in double quotes. The code that leads to production is still Chinese garbled characters! ! ! !
(If you know how to change the PyCharm configuration to solve this problem, please leave a message, thank you!!)
Since I don't know what is wrong with PyCharm, I can only solve this problem through shell scripts:
- Create a new .sh file in a directory where you can find it
I am lazy and easy to build one directly in the project directory, please don't learn from me.
2. Script content
#!/bin/bash
if [ $# -ne 3 ]; then
echo "用法: $0 <pyside6-uic路径> <输入UI文件> <输出PY文件>"
exit 1
fi
pyside6_uic="$1"
input_ui="$2"
output_py="$3"
"$pyside6_uic" "$input_ui" | ascii2uni -a U > "$output_py"
- Change the external tool configuration just created
Program: /Users/localhost/develop/PycharmProjects/sd-client-3/pyuic.sh
Arguments: /Library/Frameworks/Python.framework/Versions/3.8/bin/pyside6-uic $FileName$ $FileNameWithoutExtension$.py
Working Directory:$FileDir$
The three configurations here need to be changed according to your actual directory, don't copy from me!