Solve the problem that pyside6-uic generates py code in Chinese as unicode (garbled characters)

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 
  1. /Library/Frameworks/Python.framework/Versions/3.8/bin/pyside6-uicis your uic address
  2. LoginWindow.uiIs the address of your design UI file
  3. ui_LoginWindow.pyis the output address of the py file

The above three are customized content, and the others are fixed commands.
insert image description here

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$
insert image description here
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:

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

insert image description here
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"
  1. 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!
insert image description here

3. Solve the before and after comparison

before settlement

insert image description here

After solving:

insert image description here

Guess you like

Origin blog.csdn.net/weixin_52799373/article/details/132688102