当前位置: 首页 > 工具软件 > PySimpleGUI > 使用案例 >

pysimpleGUI

轩辕鸿
2023-12-01

一、 控件更新

1. Text

window['-fill1-'].update(value='')  #清空内容
window['-totalScore-'].update(value='%.2f' % totalScore)  #更新内容

2. Button

sg.Button('多选题', key='-type2-', size=(6, 1), font=('宋体', 11),
                                 image_data=to_base64('img/bg0.png'),
                                 border_width=0,
                                 button_color=('black', global_bgColor)), 
windows['-type2-'].update(button_color=button_color, image_data=checked_image_data) #更新按钮颜色,按钮背景图片,自己实现的返回base64图片的方法
window['-submit-'].update(disabled=True)  #按钮禁用
def to_base64(file):
    import base64
    with open(file, "rb") as f:  # 转为二进制格式
        base64_data = str(base64.b64encode(f.read()), 'utf-8')  # 使用base64进行加密

    return base64_data

checked_image_data = to_base64('img/bg0.png')

3. Radio

 sg.Frame('', [
                [sg.Text('', size=(7, 1), background_color='#E5ECFC'),
                 sg.Radio('A', 1, background_color='#E5ECFC'),
                 sg.Text(A, key='-A-', auto_size_text=True, size=(90, 1), background_color='#E5ECFC')],
                [sg.Text('', size=(7, 1), background_color='#E5ECFC'),
                 sg.Radio('B', 1, background_color='#E5ECFC'),
                 sg.Text(B, key='-B-', auto_size_text=True, size=(90, 1), background_color='#E5ECFC')],
                [sg.Text('', size=(7, 1), background_color='#E5ECFC'),
                 sg.Radio('C', 1, background_color='#E5ECFC', ),
                 sg.Text(C, key='-C-', auto_size_text=True, size=(90, 1), background_color='#E5ECFC')],
                [sg.Text('', size=(7, 1), background_color='#E5ECFC'),
                 sg.Radio('D', 1, background_color='#E5ECFC'),
                 sg.Text(D, key='-D-', auto_size_text=True, size=(90, 1), background_color='#E5ECFC')],
            ], key='-radioBox-', visible=False, border_width=0, background_color='#E5ECFC', size=(1050,200)),
windows['-radioBox-'].update(visible=False)  #更新单选按钮为不可见
window[1].reset_group()  #清空选中的单选按钮

4. CheckBox

  sg.Checkbox('A', default=False, background_color='#E5ECFC', key='-CheckboxA-'),
  sg.Text(A, key='-checkA-', auto_size_text=True, size=(65, 1), background_color='#E5ECFC')
 window['-CheckboxC-'].update(value=False)  #取消选中

5. 多行文本框

 sg.Multiline(default_text=' ', 
              background_color='#E5ECFC', 
              key='-short_answer-',
              size=(100, 6),font=custom_font )

6. 弹窗

 sg.popup_auto_close('当前为第一道题', background_color='#FFFFFF',
                                    auto_close_duration=2,
                                    no_titlebar=True,
                                    button_color=('green', '#6182FF'),
                                    text_color='green', 
                                    keep_on_top=True,
                                    relative_location=(10, 10),
                                    font=('幼圆', 13))

7. 窗口切换

 window = layout_login_window()  #初始化窗口
 window = lay_main_window(window, data_type_num_1, data_type_num_2, data_type_num_3, data_type_num_4,
                                         data_type_num_5) #需要切换窗口重新赋值给window
def layout_login_window():

    layout_login = [
        [sg.Button('登录', key='-start-', border_width=2, size=(30, 2), pad=(50, 35),
                   button_color=('black', '#108454')),],
    ]
    return sg.Window(windowName, layout_login, font=("华文细黑", 12),
                     background_color=global_bgColor,
                     icon='./img/icon.ico',
                     finalize=True)
def lay_main_window(window,radio_num, check_num, judge_num, fill_num,short_answer_num):

    sg.theme('SystemDefaultForReal')
    # ------------------- Layout Definition -------------------
    layout = [
        #这里实现页面布局
    ]

    window.close()  #关闭初始化的窗口,下面的代码返回新窗口
 
    # ------------------- Window Creation -------------------
    return sg.Window(windowName, layout, font=("华文细黑", 12),
                     background_color=global_bgColor,
                     icon='./img/icon.ico',
                     # resizable=True,
                     finalize=True
                     )

8.窗口切换

  1. 点击完OK或者Cancel后,登录页面的弹窗关闭 ;
  2. .接着创建新的Window对象,进行调用
import PySimpleGUI as sg

event, values = sg.Window('Login Window',
                  [[sg.T('Enter your Login ID'), sg.In(key='-ID-')],
                  [sg.B('OK'), sg.B('Cancel') ]]).read(close=True)

login_id = values['-ID-']
print(login_id)        

9.更新窗口内容

官网说明:
There are two important concepts when updating elements!
If you need to interact with elements prior to calling window.read() you will need to “finalize” your window first using the finalize parameter when you create your Window. “Interacting” means calling that element’s methods such as update, expand, draw_line, etc.
Your change will not be visible in the window until you either:
A. Call window.read() again
B. Call window.refresh()

需求:
在调用window.read()前更新key=‘-OUTPUT-’ 的内容

(1)运行使用如下代码

import PySimpleGUI as sg

sg.theme('BluePurple')
layout = [[sg.Text('Your typed chars appear here:'), sg.Text(size=(15,1), key='-OUTPUT-')],
          [sg.Input(key='-IN-')],
          [sg.Button('Show'), sg.Button('Exit')]]
window = sg.Window('Pattern 2B', layout)

while True:  # Event Loop
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    if event == 'Show':
        # Update the "output" text element to be the value of "input" element
        window['-OUTPUT-'].update(values['-IN-'])

window.close()

(2)结果如下:
UserWarning: You cannot Update element with key = -OUTPUT- until the window.read() is called or finalized=True when creating window warnings.warn(‘You cannot Update element with key = {} until the window.read() is called or finalized=True when creating window’.format(self.Key), UserWarning)

(3)修改内容:

window = sg.Window('Pattern 2B', layout,finalize=True)

(4)重新运行,正常

相关阅读

相关文章

相关问答