[Python GUI]DearPyGui搭建、你好、打包

0、DearPyGui认为窗口标题不需要支持中文

dpg.create_viewport(title=‘xxx’),中文编码问题 - Github议题
在这里插入图片描述

1、禁用微软商店引流

点击开始菜单输入管理应用执行别名搜索系统设置,将该列表划到最底部,找到python.exepython3.exe,将其设置为关闭状态
这是微软搞的狗皮膏药,强制型环境变量
在这里插入图片描述

2、配置Python环境

1.考虑版本管理:Anaconda官网Miniconda清华镜像pyenv-win的Github仓库
2.只安装Python:华为镜像阿里镜像newbePython官网ftp地址Python官网中文页面
3.Python嵌入版:Python3.11.2下载页面Python3.11.2嵌入版下载直链、各个镜像的嵌入式压缩包

查看setup.py判断支持的Python版本
在这里插入图片描述

(DearPyGui 不支持 Python嵌入版32位)(Python嵌入版安装Pyinstaller难度高)
本篇文章安装Python3.11.2 64位python-3.11.2-amd64.exe
勾选Add python.exe to PATH,点击Install Now
在这里插入图片描述
点击Disable path length limit,点击Close按钮,完成安装
在这里插入图片描述
运行pip config list -v获取可能的配置文件路径,参考路径创建配置文件
例如:C:\ProgramData\pip\pip.ini,新建该文件,内容如下

[global]
index-url = https://repo.huaweicloud.com/repository/pypi/simple
trusted-host = repo.huaweicloud.com
timeout = 120

3、安装DearPyGui

python -m pip install dearpyguipip install dearpygui

4、安装VSCode

如何下载安装VSCode
VSCode插件:Python
VSCode插件:Pylint
Ctrl+Shift+P选择解释器,设置python.exe的路径.\python-3.11.2-embed-amd64\python.exe
Ctrl+Shift+P选择 Linter,选择pylint

5、下载字体

MiSansOPPO Sans,然后解压备用

6、官方demo

新建文件夹dpg
新建文件showdemo.py,内容为

"""显示官方demo"""
import dearpygui.dearpygui as dpg
from dearpygui import demo

dpg.create_context()
dpg.create_viewport(title="hello", width=1020, height=600)

demo.show_demo()

dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

运行python showdemo.py
在这里插入图片描述

7、helloworld

(已知问题,该程序无法在Win10系统上正常修改窗口标题,Python版本问题)
复制MiSans-Regular.ttfdpg文件夹
新建文件.\dpg\hello.py,内容为

"""显示一个label,并添加中文支持"""
import sys
import os
import random
import threading
from ctypes import windll
import dearpygui.dearpygui as dpg


FindWindow = windll.user32.FindWindowW
SetWindowText = windll.user32.SetWindowTextW


def set_title(old_title, new_title):
    """循环检测窗口是否生成,生成之后修改窗口标题"""
    while True:
        hwnd = FindWindow(None, old_title)
        if hwnd != 0:
            SetWindowText(hwnd, new_title)
            break


TITLE = "你好1"
FONT = "MiSans-Regular.ttf"
ICON = "1.ico"
if getattr(sys, "frozen", False):
    bundle_dir = sys._MEIPASS
else:
    bundle_dir = os.path.dirname(os.path.abspath(__file__))
FONT = os.path.join(bundle_dir, FONT)
ICON = os.path.join(bundle_dir, ICON)


dpg.create_context()
RANDOM_TITLE = str(random.random())
dpg.create_viewport(
    title=RANDOM_TITLE, width=100, height=100, small_icon="1.ico", large_icon="1.ico"
)

with dpg.font_registry():
    with dpg.font(FONT, 20) as ft:
        dpg.add_font_range_hint(dpg.mvFontRangeHint_Default)
        dpg.add_font_range_hint(dpg.mvFontRangeHint_Chinese_Simplified_Common)
        dpg.add_font_range_hint(dpg.mvFontRangeHint_Chinese_Full)
    dpg.bind_font(ft)

with dpg.window(label="你好2", width=100, height=100, pos=(0, 0)):
    dpg.add_text(default_value="你好3")

dpg.setup_dearpygui()
dpg.show_viewport()
threading.Thread(target=set_title, args=(RANDOM_TITLE, TITLE)).start()
dpg.start_dearpygui()
dpg.destroy_context()

运行python hello.py
在这里插入图片描述

8、PyInstaller打包

Pyinstaller文档
请添加图片描述
在线ICO转换网站用来转换图标,PyInstaller支持修改图标

  1. pip install pyinstaller安装PyInstaller

  2. python -m PyInstaller --onefile --noconsole --name hello --icon .\1.ico .\hello.py

  3. 生成spec位置:.\dpg\hello.spec
    编辑spec文件,将datas=[],改为datas=[('MiSans-Regular.ttf', '.'), ('1.ico', '.')],
    (元组的第一个参数是当前资源路径,第二个参数是目标路径)

  4. 运行python -m PyInstaller .\hello.spec

  5. 最终程序位置:.\dpg\dist\hello.exe

  6. 运行一下hello.exe
    左上角图标正常
    在这里插入图片描述
    任务栏图标正常
    在这里插入图片描述
    文件图标正常
    在这里插入图片描述

9、3.11.2Python在Win10系统上使用SetWindowText会丢尾

修复代码(既然你喜欢丢尾巴,那我就加长尾巴,win32gui 来自 pywin32 模块)

def set_title(old_title, new_title):
    """
    循环检测窗口是否生成,生成之后修改窗口标题
    3.11.2版本Python使用SetWindowText在Window10上有bug,
    SetWindowText(123,"你好1"),窗口标题会变成"你",会丢字符
    """
    # win11系统
    if sys.getwindowsversion().build > 20000:
        hwnd = 0
        while hwnd == 0:
            hwnd = win32gui.FindWindow(None, old_title)
        while win32gui.GetWindowText(hwnd) != new_title:
            win32gui.SetWindowText(hwnd, new_title)
    # win10系统?
    else:
        hwnd = 0
        new_title_tail = new_title
        while hwnd == 0:
            print(1)
            hwnd = win32gui.FindWindow(None, old_title)
        while win32gui.GetWindowText(hwnd) != new_title:
            print(2)
            new_title_tail = new_title_tail + "_"
            win32gui.SetWindowText(hwnd, new_title_tail)

修复前
在这里插入图片描述
修复后
在这里插入图片描述

 
 
 
请添加图片描述

猜你喜欢

转载自blog.csdn.net/qq_39124701/article/details/129467929