python优秀界面_Python界面设计入门

1、PySimpleGui安装

pip install PySimpleGui

2、基本结构

import PySimpleGUI as sg # Part 1 - The import

# Define the window's contents

layout = [ [sg.Text("What's your name?")], # Part 2 - The Layout

[sg.Input()],

[sg.Button('Ok')] ]

# Create the window

window = sg.Window('Window Title', layout) # Part 3 - Window Defintion

# Display and interact with the Window

event, values = window.read() # Part 4 - Event loop or Window.read call

# Do something with the information gathered

print('Hello', values[0], "! Thanks for trying PySimpleGUI")

# Finish up by removing from the screen

window.close()

image.png

由此可见:窗口元素布局取决于一个列表:layout。所有的元素和元素排列顺序都在这里。当然,字体大小,文本框大小等等,也可以在这里设置,这是后话。

window = sg.Window('Window Title', layout)

这句是把窗口中的元素布局在窗口中实现。名称就是:字符串'Window Title'。

event, values = window.read()

这是从窗口接受消息,后面好判断,这是个循环,一旦接收到消息就结束了。剩下的工作就是对这种消息的处理。一般情况下要把这句代码写入一个死循环里,才能保证程序一直在运行。就可以不断的接受窗口消息,然后处理。

One-shot Window

窗口界面一共分两大类。其一,就是这种One-shot 类;其二,是Persistent 类。第一类是直出现一次的窗口,输入一个信息或者显示一个信息,然后完成了它的历史使命,消失。第二类,就是一只存在的窗口,就是主界面。他在程序运行的过程中必须一直存在。可以和User互动。

上面举得例子就是第一类,One-shot类。

import PySimpleGUI as sg

layout = [[sg.Text('My one-shot window.')],

[sg.InputText(key='-IN-')],

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

window = sg.Window('Window Title', layout)

event, values = window.read()

window.close()

text_input = values['-IN-']

sg.popup('You entered', text_input)

这里的key='-IN-',就是为这个窗口元素设置一个键,方便后面引用。比如后面那句:text_input = values['-IN-']。如果不设定键的话,系统会自动给它分配一个数字作为键。上面这段程序还可以改写成类似下面的样子,更加简短。

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-']

这里有一句close = True,就是在read返回之前,会关闭窗口。

Persistent Window

import PySimpleGUI as sg

sg.theme('DarkAmber') # Keep things interesting for your users

layout = [[sg.Text('Persistent window')],

[sg.Input(key='-IN-')],

[sg.Button('Read'), sg.Exit()]]

window = sg.Window('Window that stays open', layout)

while True: # The Event Loop

event, values = window.read()

print(event, values)

if event == sg.WIN_CLOSED or event == 'Exit':

break

window.close()

image.png

这里的while True:语句成为关键。使得系统可以一直接受窗口的信息,这样的窗口一直存在,直到手动关闭为止。

运行结果如下:

Read {'-IN-': 'typed into input field'}

Read {'-IN-': 'More typing'}

Exit {'-IN-': 'clicking the exit button this time'}

这里的Read和Exit就是Event,大括号里的内容就是values,是个字典,有key有value。如果是点击右上角的X来关闭Window的,那Event和values都是None。

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()

print(event, values)

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()

image.png

这个窗口中输入任何文本信息,然后点击Show按钮,文字就会出现在第一行。这个功能如何实现呢?就是代码中的一句:window['-OUTPUT-'].update(values['-IN-'])。这里的'-OUTPUT-'是一个Text的键,update自然是更新的意思,更新的内容就放在括号里。这里放的是另外一个键的value,就是输入框。

The primary suggested conventions are:

import PySimpleGUI as sg

Name your Window window

Name the return values from reading your window event and values

Name your layout layout

Use window[key] to lookup elements

For keys that are strings, follow this pattern '-KEY-'

2. 主题:窗口美化

image.png

sg.theme('Dark Green 5')

各花入各眼。个人喜好不同,选择也不同。这个就不展开了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值