python gui(六)全局设定

1、菜单栏 Menu

sg.Menu()

Menu(
	menu_definition,
	# 菜单栏定义
    tearoff=False,
    # 独立菜单设定,设定为True后相应的下拉菜单中显示“------”的虚线,点击虚线相应的菜单以独立窗口的方式显示。
    font=None,
    # 字体名称大小设定
    pad=None,
    # 周围元素间隔设定
    key=None,
    # 唯一标识符设定
      )

在这里插入图片描述

关于menu_definition示例


menu_def = [['&File', ['!&New File', '&Open File::openkey', '---', 'Open Fol&der', 'E&xit']],
            ['!&Edit', ['!&Paste', ['Cut', 'Repeat', ], 'Undo'], ],
            ['&Find', ['Find...', 'Replace']],
            ['&Tools', ['Command &1', 'Command &2', 'Command &3', 'Command &4']],
            ['&Help', '&About...'], ]

重点

‘—’ 菜单栏分割线设定

& 快捷键标注 Alt+key(避免重复) &File其快捷键对应的是F,F&ind其对应的快捷键对应的是i,即&后面紧邻的字符为快捷键

! 菜单栏禁用设定

:: 菜单key设定。作为事件的一部分。 (给具体的某个键设定key值,例如:file::file_key,此时点击file时,返回的事件名称为file::file_key)

updata方法

Update(
		menu_definition=None,
 		visible=None
 		)
import PySimpleGUI as sg


menu_def = [['&File', ['!&New File', '&Open File::openkey', '---', 'Open Fol&der', 'E&xit']],
            ['!&Edit', ['!&Paste', ['Cut', 'Repeat', ], 'Undo'], ],
            ['F&ind', ['Find...', 'Replace']],
            ['&Tools', ['Command &1', 'Command &2', 'Command &3', 'Command &4']],
            ['&Help', '&About...'], ]


layout=[[sg.Menu(menu_def,
    key='UU')]]+[[sg.In(key=i)]for i in range(5)]
window=sg.Window('Python GUI',layout,keep_on_top=True)

while True:
    event,values=window.read()
    print(event)   
    if event==None:  
        break

window.close()

import  PySimpleGUI as sg

menu_def = [['&File', ['!&New File', '&Open File::openkey', '---', 'Open Fol&der', 'E&xit']],
            ['!&Edit', ['!&Paste', ['Cut', 'Repeat', ], 'Undo'], ],
            ['&Find', ['Find...', 'Replace::Replac2']],
            ['&Tools', ['Command &1', 'Command &2', 'Command &3', 'Command &4']],
            ['&Help', '&About...'], ]
layout = [[sg.In(i)] for i in range(5)]+\
         [[sg.Menu(menu_def,
                   tearoff=True,
                   )]]   # 虽然menu放在In后面,但是依然显示在最上面一排,

windows = sg.Window('pysimplegui',layout)

while True:
    event, values = windows.read()
    print(event)
    if event==None:
        break
    if event == 'Replace::Replac2':
        sg.popup("点击替换")

    if event == "Command 1":
        sg.popup("点击"+event)



2、window窗口常用属性


Window(
	title,
	# 窗口标题设定
    layout=None,
    # 布局设定
    location=(None, None),
    # 默认窗口出现的位置
    size=(None, None),   
    # 窗口宽高设定,未设定的情况下,根据元素大小自行设定
    element_padding=None,
    # 元素间间隔设定
    button_color=None,
    # 按钮元素颜色设定(文本颜色,背景颜色)
    font=None,
    #(字体名称,字体大小)
    background_color=None,
    # 背景颜色设定
    auto_close=False,
    # 界面自动关闭设定
    auto_close_duration=3,
    # 默认为3秒,自动关闭前界面持续打开时间
    no_titlebar=False,
    # 界面无标题栏设定,没有标题栏的时候一定给他定义一个关闭窗口的事件,例如定义一个cancel按钮
    grab_anywhere=False,
    # 拖拽窗口设定。
    keep_on_top=False,
    # 如果为True,界面保持在屏幕的最前方
    resizable=False,
    # 如果为True,界面生成后可以调整大小
    disable_close=False,
    # 如果为True,窗口关闭按钮将不起作用
    disable_minimize=False,
    # 如果为True,窗口最小化按钮将不起作用
    right_click_menu=None,
    # 右击调出菜单
    transparent_color=None,
    # 界面透明设定 =sg.theme_background_color()
    element_justification="left",
    # 元素对齐方式设定。有效值 left,right,center,相对的是窗口
		)

在这里插入图片描述

在这里插入图片描述

import  PySimpleGUI as sg

menu_def = [['&File', ['!&New File', '&Open File::openkey', '---', 'Open Fol&der', 'E&xit']],
            ['!&Edit', ['!&Paste', ['Cut', 'Repeat', ], 'Undo'], ],
            ['&Find', ['Find...', 'Replace::Replac2']],
            ['&Tools', ['Command &1', 'Command &2', 'Command &3', 'Command &4']],
            ['&Help', '&About...'], ]
layout = [[sg.In(i)] for i in range(5)]+\
         [[sg.Menu(menu_def,
                   tearoff=True,
                   )]]   # 虽然menu放在In后面,但是依然显示在最上面一排,

windows = sg.Window('pysimplegui',layout,
                    size=(500,500),
                    # transparent_color=sg.theme_background_color(),
                    element_justification="right"
                    )

while True:
    event, values = windows.read()
    print(event)
    if event==None:
        break
    if event == 'Replace::Replac2':
        sg.popup("点击替换")

    if event == "Command 1":
        sg.popup("点击"+event)



3、Multiline多行文本框

既可以用来输入,也可以用来输出。

sg.Multiline()orsg.ML()

Multiline(
	default_text="",
    # 默认显示的文本
    disabled=False,
    # 元素禁用状态设定
    border_width=None,
    # 边界线条宽度设定
    size=(None, None),
    # 宽度和行高设定
    background_color=None,
    # 背景颜色设定
    text_color=None,
    # 文本颜色设定
    enable_events=False,
    # 元素事件属性
    key=None,
    # 元素的唯一标识符
    write_only=False,
    # 设定为True时,文本框只提供用户写入,窗口不读取。无返回值。
    reroute_stdout=False,
    # print语句内容会显示在此文本框内
    reroute_cprint=False,
    # 使用cprint将内容打印到此文本框内。详细参考sg.cprint()
    reroute_stderr=False,
    # 捕捉异常时将文本写在此元素内. sys.stderr.write('?'),一般用不到,可以使用上面两种输出方式加上print()方法就可以了。
    autoscroll=False,
    # 如果为True,更多数据添加到末尾时元素的内容将自动滚动
    focus=False,
    # 焦点设置
    font=None,
    # 字体名称和大小设定
    pad=None,
    # 和周围元素间间隔的设定
    tooltip=None,
    # 悬浮文本设定
    justification=None,
    # 对齐方式设定
    right_click_menu=None,
    # 右击调出菜单设定
    visible=True,
    # 元素可见状态设定。
    do_not_clear=True,
    # 默认为True,如果设定为False,窗口读取一次,内容就会自动清除。默认True,不要与enable_events同时设定
     )

信息补充:print方法的参数分割符sep和终止符end

a = 'a'
b = 'b'
print("===========sep分割符,===========")
print(a,b)   # 默认分割符有一个空格,所以输出a和b之间有一个空格
print(a,b,sep="")   # 修改了分割符,所以输出a和b之间没有空格
print(a,b,sep="+")
print("===========end终止符,===========")
print(a)     # 每个print语句默认增加一个换行符
print(a,end="\n")   # 等效print(a)
print("===========修改终止符===========")
print(a,end='---')  #相当是将默认的终止符”\n“修改为”-----“
print(b)            # 因为上面的打印语句修改了终止符,因此语句紧跟上面语句输出

在这里插入图片描述

原来print打印的内容显示在sublime输出控制台上。

设定了reroute_stdout=True,之后
print打印内容会显示在多行文本框内。
则编译器的输出控制台不会显示。

设定reroute_cprint=True,
print打印内容会继续显示到sublime输出控制台。
使用sg.cprint()可以讲内容打印到多行文本框。

cprint方法


cprint(
    args=*<1 or N object>,
    # 可变参数,
    end=None,
    # 结束符  默认为回车键
    sep=" ",
    # 分隔符 默认为空格键
    text_color=None,
    # 可以简写成t
    background_color=None,
    # 可以简写成b
    colors=None,
    # 可以简写成c
    key=None,
    #元素的唯一标识符
    justification=None
    #文本对齐方式)

update方法

Update(
    value=None,
    # 更新内容
    disabled=None,
    # 元素禁用
    append=False,
    # 如果设定为True,要更新的内容会接在原来文本的下方。原来的文本不会消失。
    font=None,
    # 字体大小名称设定
    text_color=None,
    # 文本颜色设定
    background_color=None,
    # 背景颜色设定
    text_color_for_value=None,
    # 更新的文本的字体颜色设定
    background_color_for_value=None,
    # 更新的文本的背景颜色设定
    visible=None,
    # 元素可见状态更新。默认可见
    autoscroll=None,
    # 更新文本是否自动滚动。默认不自动滚动
    justification=None
    # 更新文本对齐方式)
import  PySimpleGUI as sg


layout = [[sg.ML(
    default_text="您好",
    # disabled=True,
    # border_width=10,
    size=(50,10),   # 行高10,代表10行
    background_color="red",
    text_color="yellow",
    enable_events=True,
    key='-ML-',
    write_only=False,  # 当为True时,只能写入但是无法读写,即不返回多行文本框中内容
    # reroute_stdout=True, #print打印语句会输出到多行文本框中
    reroute_cprint=True,
    autoscroll=True,
    do_not_clear=False,  # 默认True,不要与enable_events同时设定

)],
    [sg.Ok(),sg.Cancel()]

]
windows = sg.Window('pysimplegui',layout,
                    # size=(500,500),
                    # transparent_color=sg.theme_background_color(),
                    # element_justification="right"

                    )

while True:
    event, values = windows.read()
    # print(event)
    if event==None:
        break
    if event=="Ok":
        windows['-ML-'].update("大家好",
                               disabled=True,
                               append=True,
                               text_color="red",
                               text_color_for_value="black",
                               autoscroll=True,
                               justification="left"
                               )
    if event == 'Cancel':
        sg.cprint("Cancel",
                  "--From cancel button",
                  end=";",sep='++++++',
                  # text_color="balck",
                  # background_color='white',
                  # colors=("yellow","white"),
                  justification='r',

                  )

print方法

window[key].print()


print(
    args=*<1 or N object>,
    # 可变参数
    end=None,
    # 要打印的元素的结束符 默认为换行 end='\n'
    sep=None,
    # 要打印的元素的分隔符 默认为空格 sep=' '
    text_color=None,
    # 要打印的元素文本颜色
    background_color=None,
    # 要打印的元素这一行的背景颜色
    justification=None
    # 要打印的元素对齐方式
    )

import  PySimpleGUI as sg


layout = [[sg.ML(
    default_text="您好",
    # disabled=True,
    # border_width=10,
    size=(50,10),   # 行高10,代表10行
    # background_color="red",
    # text_color="yellow",
    enable_events=True,
    key='-ML-',
    write_only=False,  # 当为True时,只能写入但是无法读写,即不返回多行文本框中内容
    # reroute_stdout=True, #print打印语句会输出到多行文本框中
    reroute_cprint=True,
    autoscroll=True,
    do_not_clear=False,  # 默认True,不要与enable_events同时设定

)],
    [sg.Ok(),sg.Cancel()]

]
windows = sg.Window('pysimplegui',layout,
                    # size=(500,500),
                    # transparent_color=sg.theme_background_color(),
                    # element_justification="right"

                    )

while True:
    event, values = windows.read()
    # print(event)
    if event==None:
        break
    if event=="Ok":
        windows['-ML-'].print("123",justification='left',
                              text_color="red",
                              background_color="pink"

                              )
    if event == 'Cancel':
        windows['-ML-'].print("456",justification='right',
                              text_color='blue',
                              background_color="grey"
                              )

垂直分割线


sg.VerticalSeparator(
    color=None,
    key=None,
    pad=None
    )

对话框小案例

import PySimpleGUI as sg

layout=[[sg.ML(size=(60,10),reroute_cprint=True,autoscroll=True,key='-ML-')],
        [sg.In('你好!',key='-Input_A-',size=(20,1)),sg.B('Andy'),
        sg.VerticalSeparator(color='red'),
        sg.In('你好!',key='-Input_B-',size=(20,1)),sg.B('Amy')]]


window=sg.Window('Python GUI',layout,keep_on_top=True)

while True:
    event,values=window.read()

    if event is None:  
        break

    if event=='Andy':
        sg.cprint(values['-Input_A-'],'From Andy',c=('red','yellow'),justification='l',sep='  ——')
    if event=='Amy':
        sg.cprint(values['-Input_B-'],'From Amy',t='blue',justification='r',sep='  ——')

window.close()
  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值