ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python-PySimpleGUI

2022-01-24 01:02:33  阅读:201  来源: 互联网

标签:Python Text update PySimpleGUI window Part sg event


#***************************************************************模板******************************************************
'''import PySimpleGUI as sg            # Part 1 - 导入库
  
# 定义窗口的内容
layout = [ [sg.Text("请输入基本信息")],   # Part 2 - 排版
      [sg.Text("姓名"),sg.Input("张明明")],
      [sg.Text("性别"),sg.Input("男")],
      [sg.Text("国籍"),sg.Input("中国")],
      [sg.Button('确认'),sg.Button('取消')] ]
  
# 创建窗口
window = sg.Window('Window Title', layout)   # Part 3 - 窗口定义
                         
# Display and interact with the Window
while True:
    event, values = window.read()          # Part 4 - 开启主循环 window.read()
    if event==None:
        break
    if event =="确认":
        # Do something with the information gathered
        sg.Popup('Hello', values[0], "! Thanks for trying PySimpleGUI")
    if event =="取消":
        # Do something with the information gathered
        sg.Popup('Hello', values[0], "! Thanks for trying PySimpleGUI")
# Finish up by removing from the screen
window.close() # Part 5 - 关闭窗口

'''



#**********************文本*******************************************
'''
import PySimpleGUI as sg            # Part 1 - 导入库
  
#text = 优美胜于丑陋(Python 以编写优美的代码为目标)
#明了胜于晦涩(优美的代码应当是明了的,命名规范,风格相似)
#简洁胜于复杂(优美的代码应当是简洁的,不要有复杂的内部实现)
#复杂胜于凌乱(如果复杂不可避免,那代码间也不能有难懂的关系,要保持接口简洁)
#扁平胜于嵌套(优美的代码应当是扁平的,不能有太多的嵌套)
#间隔胜于紧凑(优美的代码有适当的间隔,不要奢望一行代码解决问题)
#可读性很重要(优美的代码是可读的)
#即便假借特例的实用性之名,也不可违背这些规则(这些规则至高无上)
#
# 定义窗口的内容
layout = [[sg.T(text,
    key='-TEXT-',
    size=(50,20),
    font=('黑体',20),
    auto_size_text=True,
    enable_events=False,
    relief='solid',
    border_width=5,
    text_color='red',
    background_color='white',
    justification='r',
    pad=None,
    right_click_menu=['1',['1',['1-1','1-2'],'2','3']],
          )],
[sg.Button('确认'),sg.Button('取消')] 
    ]

  
# 创建窗口
window = sg.Window('Window Title', layout)   # Part 3 - 窗口定义
                         
# Display and interact with the Window
while True:
    event, values = window.read()          # Part 4 - 开启主循环 window.read()
    print(event)
    if event==None:
        break
    if event=='确认':
        break
# Finish up by removing from the screen
window.close() # Part 5 - 关闭窗口
'''

############################################文本输入框#############################
'''import PySimpleGUI as sg            # Part 1 - 导入库
  
# 定义窗口的内容
layout = [ [sg.Text("请输入基本信息")],   # Part 2 - 排版
      [sg.Text("姓名",key='-NAME-'),sg.Input("张明明")],
      [sg.Text("性别"),sg.Input("男")],
      [sg.Text("国籍"),sg.Input("中国")],
      [sg.Button('更改姓名的颜色'),sg.Button('取消')] ]
  
# 创建窗口
window = sg.Window('Window Title', layout)   # Part 3 - 窗口定义
                         
# Display and interact with the Window
while True:
    event, values = window.read()          # Part 4 - 开启主循环 window.read()
    if event==None:
        break
    if event =="更改姓名的颜色":
        # Do something with the information gathered
        window['-NAME-'].update(
        value='Name',
        background_color='grey',
        text_color='red',
        visible=''
        )
    if event =="取消":
        # Do something with the information gathered
        sg.Popup('Hello', values[0], "! Thanks for trying PySimpleGUI")
# Finish up by removing from the screen
window.close() # Part 5 - 关闭窗口'''
##############################################文本3##########################################


import PySimpleGUI as sg            # Part 1 - 导入库
  
# 定义窗口的内容
layout = [ [sg.Button('中文'),sg.Button('English')] ,
      [sg.Text("请输入基本信息",key='-TEXT3-')],  
      [sg.Text("姓名",key='-TEXT4-'),sg.Input("张明明")],
      [sg.Text("性别",key='-TEXT5-'),sg.Input("男")],
      [sg.Text("国籍",key='-TEXT6-'),sg.Input("中国")],
      [sg.Button('确认',key='-TEXT7-'), sg.Button('取消',key='-TEXT8-')], ]
  
# 创建窗口
window = sg.Window('Window Title', layout)   # Part 3 - 窗口定义
                         
# Display and interact with the Window
while True:
    event, values = window.read()          # Part 4 - 开启主循环 window.read()
    print(event)
    if event==None:
        break
    if event =='English':
        window['-TEXT3-'].update(value='Pls input your name')
        window['-TEXT4-'].update(value='Name')
        window['-TEXT5-'].update(value='Sex')
        window['-TEXT6-'].update(value='Nation')
        window['-TEXT7-'].update('Confirm')
        window['-TEXT8-'].update('Cancle')
    if event =='中文':
        window['-TEXT3-'].update(value='请输入个人信息')
        window['-TEXT4-'].update(value='姓名')
        window['-TEXT5-'].update(value='性别')
        window['-TEXT6-'].update(value='国籍')
        window['-TEXT7-'].update('确认')
        window['-TEXT8-'].update('取消')


# Finish up by removing from the screen
window.close() # Part 5 - 关闭窗口

  

标签:Python,Text,update,PySimpleGUI,window,Part,sg,event
来源: https://www.cnblogs.com/hiroren/p/15837977.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有