DearPyGui icon indicating copy to clipboard operation
DearPyGui copied to clipboard

dpg.create_viewport(title='xxx'),Chinese coding problem

Open monkeycc opened this issue 2 years ago • 3 comments


import dearpygui.dearpygui as dpg

dpg.create_context()
dpg.create_viewport(title='我是中文', width=600, height=200)
dpg.setup_dearpygui()



with dpg.font_registry():
    with dpg.font(r"D:\0SDXX\DearPyGui\MiSans-Medium.ttf", 20) as default_font:
        dpg.add_font_range_hint(dpg.mvFontRangeHint_Chinese_Full)
dpg.bind_font(default_font) # Binds the font globally


with dpg.window(label="我是中国文字"):
    dpg.add_text("你好, 世界")

dpg.show_viewport()

# below replaces, start_dearpygui()
while dpg.is_dearpygui_running():
    # insert here any code you would like to run in the render loop
    # you can manually stop by using stop_dearpygui()
    print("this will run every frame")
    dpg.render_dearpygui_frame()

dpg.destroy_context()
微信截图_20220511003735

monkeycc avatar May 10 '22 16:05 monkeycc

The author probably does not convert unicode python str to wide char in the underlying windowing managment

yhyu13 avatar Jun 30 '22 16:06 yhyu13

If you realy need that, you may try to modify the source (for Windows) as shown below. It is a dirty solution without knowledge of all consequences but it is working for me. Maybe the correct fix should modify also the CreateWindow() call in the mvShowViewport(). I do not know.

src\platform\Windows\mvViewport.cpp:

#include <locale>
#include <codecvt>
#include "mvWindowsSpecifics.h"
...
std::wstring convert_to_wstring(const std::string& str)
{
	std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> conv;
	return conv.from_bytes(str);
}
...
mv_internal void
mvPrerender(mvViewport& viewport)
{
...
	if (viewport.titleDirty)
	{
		SetWindowTextW(viewportData->handle, convert_to_wstring(viewport.title).c_str());
		viewport.titleDirty = false;
	}
...
}

sedenka avatar Aug 16 '22 20:08 sedenka

Possible duplicate of #1667.

sedenka avatar Aug 26 '22 10:08 sedenka

Workaround provided and possibly a duplicate. Closing the issue.

bandit-masked avatar Jan 05 '23 00:01 bandit-masked

Hi, Why is this not included in the release? For me this is also a problem. And I have seen the same issues arise for other languages. Solution not actual for current 1.9.1/1.9.2 version

trancefer avatar May 30 '23 17:05 trancefer