pysimplegui实现类似richtextbox的功能-富文本功能

pysimplegui实现类似richtextbox的功能-富文本功能,第1张

pysimplegui实现类似richtextbox的功能-富文本功能
import PySimpleGUI as sg

def jiancha1():#定义检查函数,并且向window1输出富文本内容
    
    #window1['-MLINE1-'].update(out)
    window1['-MLINE1-'].print("检查完成第一步",text_color='red',background_color='yellow',end="")#输出富文本内容,并且不会自动换行,富文本作用范围在结束符之后
    window1['-MLINE1-'].print("n检查完成第二步",text_color='red',background_color='white',font=('宋体', 20,'bold overstrike'),end="")#字体作用在结束符后面,不会自动换行
    #window1['-MLINE1-'].update(out)
    #sg.Print("检查完成第三步",text_color='red')#调试窗口d出
    #详细的字体设置
    '''
    Font
Specifies the font family, size, and style. Font families on Windows include: * Arial * Courier * Comic, * Fixedsys * Times * Verdana * Helvetica (the default I think)

The fonts will vary from system to system, however, Tk 8.0 automatically maps Courier, Helvetica and Times to their corresponding native family names on all platforms. Also, font families cannot cause a font specification to fail on Tk 8.0 and greater.

If you wish to leave the font family set to the default, you can put anything not a font name as the family. The PySimpleGUI Demo programs and documentation use the family 'Any' to demonstrate this fact.. You could use "default" if that's more clear to you.

There are 2 formats that can be used to specify a font... a string, and a tuple Tuple - (family, size, styles) String - "Family Size Styles"

To specify an underlined, Helvetica font with a size of 15 the values: ('Helvetica', 15, 'underline italics') 'Helvetica 15 underline italics'

Font Style - Valid font styles include:

italic
roman
bold
normal
underline
overstrike
'''
    

sg.theme('Dark Blue 3')  # 设置窗口主题的颜色主题
#设置各个控件的布局
layout = [[sg.Text('Your typed chars appear here:'), sg.Text(size=(12,1), key='-OUTPUT-')],#文本显示框,索引key为-OUTPUT-,大小为12*1
          [sg.Input(key='-IN-')],   #文本输入框,索引key为-IN-       
          [sg.Text("结果输出:")],#文本输入框
          [sg.Multiline(size=(120,24), key='-MLINE1-',font=('宋体', 12))],#多行输入输出框,索引key为-MLINE1-,大小为120*24,并且设置默认字体
          [sg.Text("处理进度:")],
          [sg.Multiline(size=(120,8), key='-MLINE2-',font=('宋体', 12))],#多行输入输出框,索引key为-MLINE1-,大小为120*24,并且设置默认字体
          [sg.Push(),sg.Button('开始检查',font=('宋体', 12)), sg.Button('退出',font=('宋体', 12)),sg.Push()]]#将两个按钮居中,其中sg.Push()表示空白控件占位

window1 = sg.Window('检查助手', layout)#创建窗口
while True:  # Event Loop
    event, values = window1.read()
    print(event, values)
    if event == sg.WIN_CLOSED or event == '退出':
        break
    if event == '开始检查':#当点击开始检查按钮时,调用jiancha1函数,并且开启新的窗口
        # change the "output" element to be the value of "input" element
        window1['-MLINE1-'].update(values['-IN-'])
        jiancha1()#调用第一个检查函数
        #创建一个新的窗口,用于输入或者展示其他内容
        sg.theme('Dark Grey 13')
        layout2 = [[sg.Text('Filename')],
                [sg.Input(), sg.FileBrowse()],
                [sg.OK(), sg.Cancel()]]
        window2 = sg.Window('Get filename example', layout2)
        event, values = window2.read()
        window2.close()
        #新窗口创建结束       

window1.close()

 简介,大家都知道pysimplegui极大的简化了python的gui界面制作,但是在查阅官方文档的时候,发现官方文档并没有提供详细的富文本显示方法,经过学习消化之后,制作了类似于richtextbox的模板代码文件,虽然算是基础功能,但是用于简单的办公自动化检测相关方面的处理已经绰绰有余了,有了上述内容,基本上,就实现了类似富文本的内容,还是很方便的。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5720756.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存