在PySimpleGui中写了一个popup弹窗,用属性custom_text=('确认', '取消') 修改按钮上的文字运行时按钮上的字异常显示,但是

弹窗上的按钮文字是英文时显示正常,但是改成中文就显示异常了,有没有什么好的办法解决,一开始以为是文字纵向排列了,但是仔细看过弹窗的属性没有文字横向排列的方法,声明编码也试过没有效果

代码如下:

import PySimpleGUI as pg
pg.set_options(font=('宋体', 12))
layout = [
    [pg.T('用 户 名:'),pg.In('',size=(15,0),key='-name-'),],
    [pg.T('新 密 码:'),pg.In('',size=(15,0),key='-new_pwd-'),],
    [pg.T('确认密码:'),pg.In('',size=(15,0),key='-new_pwd2-'),],
    [pg.B('确认修改',pad=((40,40),(0,0)),),pg.B('取消')]
]

wd = pg.Window("密码修改", layout, grab_anywhere=True)
while True:
    event, values = wd.read()
    if event in(None,'取消') :
        break

    if event == '确认修改':
        b = pg.popup('确定要修改密码吗?',custom_text=('确认', '取消'),keep_on_top=True,)
        if b=='确认':
            print('修改成功')

wd.close()

下图是按钮为中文是运行图片:
在PySimpleGui中写了一个popup弹窗,用属性custom_text=('确认', '取消')  修改按钮上的文字运行时按钮上的字异常显示,但是
下图是按钮为英文时运行图片:
在PySimpleGui中写了一个popup弹窗,用属性custom_text=('确认', '取消')  修改按钮上的文字运行时按钮上的字异常显示,但是

Jason990420
最佳答案

源代码使用内建函数len方式取其宽度, 所以按钮的宽度只有2, 容不下中文两个字, 没有别的方法, 只能自己定义一个.

import PySimpleGUI as sg

sg.set_options(font=('宋体', 12))

# Old one
sg.popup('确定要修改密码吗?', custom_text=('确认', '取消'))

# New one
def popup(message, title=None, buttons=('确认', '取消'), modal=True):
    layout = [
        [sg.Text(message)],
        [sg.Button(button) for button in buttons]
    ]
    window = sg.Window(title if title else message, layout, modal=modal)
    button, values = window.read(close=True)
    return button

popup('确定要修改密码吗?', buttons=('确认', '取消'))

file file

6个月前 评论
讨论数量: 2

这个问题在网上查了很久,没有收获,翻到这个论坛进来看看大佬们有没有遇到过这个问题

6个月前 评论
Jason990420

源代码使用内建函数len方式取其宽度, 所以按钮的宽度只有2, 容不下中文两个字, 没有别的方法, 只能自己定义一个.

import PySimpleGUI as sg

sg.set_options(font=('宋体', 12))

# Old one
sg.popup('确定要修改密码吗?', custom_text=('确认', '取消'))

# New one
def popup(message, title=None, buttons=('确认', '取消'), modal=True):
    layout = [
        [sg.Text(message)],
        [sg.Button(button) for button in buttons]
    ]
    window = sg.Window(title if title else message, layout, modal=modal)
    button, values = window.read(close=True)
    return button

popup('确定要修改密码吗?', buttons=('确认', '取消'))

file file

6个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!