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

神器 PySimpleGUI 初体验

厉念
2023-12-01

PySimpleGUI 什么时候有用呢?显然,是你需要 GUI 的时候。仅需不超过 5 分钟,就可以让你创建并尝试 GUI。最便捷的 GUI 创建方式就是从 PySimpleGUI 经典实例中拷贝一份代码============》直接看下面的例程就可以了

  • 使用手册(官方手册)==》https://pysimplegui.readthedocs.io/en/latest/cookbook/
  • 安装

pip install pysimplegui
or
pip3 install pysimplegui

或者自己下载,链接:PyPI

以下就是初次体验的例程
#!usr/bin/env python
# -*-coding:utf-8 -*-
# @Time      :2019/10/15 17:14
# @Author    :xxx
# @File      :1-体验.py
# description:
# @SoftWare  :PyCharm Community Edition



import PySimpleGUI as sg

def test_1():
	'''
	简单测试PySimpleGUI
	:return:
	'''
	# All the stuff inside your window.=================1
	layout = [
		[sg.Text('some text on row1')],
		[sg.Text('enter something on row2'), sg.InputText()],
		[sg.Button('ok'), sg.Button('cancel')],

	]

	# Create the Window=================================2
	window = sg.Window('window titel', layout=layout)

	# Event Loop to process "events" and get the "values" of the inputs
	while True:
		event, values = window.read() #====================3
		# if user closes window or clicks cancel
		if event in (None, 'cancel'):
			break;
		print('you entered:', values[0])

	window.close()



def test_2():
	'''
	测试 PySimpleGUI 排版
	:return:
	'''

	layout = [
		[sg.Text('Please enter your Name, Address, Phone')],
		[sg.Text('Name', size=(15, 1)), sg.InputText('name')],
		[sg.Text('Address', size=(15, 1)), sg.InputText('address')],
		[sg.Text('Phone', size=(15, 1)), sg.InputText('phone')],
		[sg.Submit(), sg.Cancel()]
	]

	# Create the Window
	window = sg.Window('window titel', layout=layout)

	button, values = window.read()

	print(button, values[0], values[1], values[2])



def test_3():
	'''
	基本的GUI 组件
	:return:
	'''
	# 外观
	sg.ChangeLookAndFeel('GreenTan')

	# All the stuff inside your window.=================1
	column1 = [
		[sg.Text('column 1', background_color='#d3dfda', justification='center', size=(10,1))],
		[sg.Spin(values=('Spin Box 1', '2','3'), initial_value='Spin Box 1')],
		[sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 2')],
		[sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 3')],

	]

	layout = [
		# 文本
		[sg.Text('All graphic widgets in one form!', size=(30, 1), font=("Helvetica", 25))],
		[sg.Text('Here is some text.... and a place to enter text')],
		# 默认输入框
		[sg.InputText('This is my text')],
		# 检查框
		[sg.Checkbox('My first checkbox!'), sg.Checkbox('My second checkbox!', default=True)],
		# 圆角框
		[sg.Radio('My first Radio!     ', "RADIO1", default=True), sg.Radio('My second Radio!', "RADIO1")],
		# 多行文本
		[sg.Multiline(default_text='This is the default Text should you decide not to type anything', size=(35, 3)),
		 sg.Multiline(default_text='A second multi-line', size=(35, 3))],
		# 下拉框
		[sg.InputCombo(('Combobox 1', 'Combobox 2'), size=(20, 3))],
		# 水平 拖动条
		[sg.Slider(range=(1, 100), orientation='h', size=(34, 20), default_value=85)],
		# 垂直 拖动条
		[sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=25),
		 sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=75),
		 sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=10)],
		# 列表框
		[sg.Listbox(values=('Listbox 1', 'Listbox 2', 'Listbox 3'), size=(30, 3))],
		# 整列
		[sg.Column(column1, background_color='#FF3E96')],
		# 横线
		[sg.Text('_'  * 80)],
		# 文本
		[sg.Text('Choose A Folder', size=(35, 1))],
		# 文件浏览器
		[sg.Text('Your Folder', size=(15, 1), auto_size_text=False, justification='right'),
		 sg.InputText('Default Folder'), sg.FolderBrowse()],
		[sg.Submit(), sg.Cancel()],
		[sg.Button('ok'), sg.Button('cancel')],

	]


	# Create the Window=================================2
	window = sg.Window('window titel', layout=layout, default_element_size=(40,1))

	# Event Loop to process "events" and get the "values" of the inputs
	while True:
		event, values = window.read()  # ====================3
		# if user closes window or clicks cancel
		if event in (None, 'cancel'):
			break;
		# 打印
		# event---按钮事件, values 是字典,所有可输入的值
		print(event, values)

		# 弹窗提示
		# sg.Popup(event, values)

	window.close()


def test_4():
	'''
	基本的GUI 组件
	:return:
	'''
	# 外观
	print(sg.ListOfLookAndFeelValues())
	'''
	['SystemDefault', 'Reddit', 'Topanga', 'GreenTan', 'Dark', 'LightGreen', 'Dark2', 'Black', 'Tan', 'TanBlue', 
	'DarkTanBlue', 'DarkAmber', 'DarkBlue', 'Reds', 'Green', 'BluePurple', 'Purple', 'BlueMono', 'GreenMono', 
	'BrownBlue', 'BrightColors', 'NeutralBlue', 'Kayak', 'SandyBeach', 'TealMono']
	'''

	# 改变外观
	sg.ChangeLookAndFeel('GreenTan')

	# 组合列1
	column1 = [
		[sg.Text('column 1', background_color='#d3dfda', justification='center', size=(10,1))],
		[sg.Spin(values=('Spin Box 1', '2','3'), initial_value='Spin Box 1')],
		[sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 2')],
		[sg.Spin(values=('Spin Box 1', '2', '3'), initial_value='Spin Box 3')],

	]

	# 布局设计
	layout = [
		# 文本标签
		[sg.Text('All graphic widgets in one form!', size=(30, 1), font=("Helvetica", 25))],
		[sg.Text('Here is some text.... and a place to enter text')],
		# 默认输入框
		[sg.InputText('This is my text')],
		# 检查框
		[sg.Checkbox('My first checkbox!'), sg.Checkbox('My second checkbox!', default=True)],
		# 圆角框
		[sg.Radio('My first Radio!     ', "RADIO1", default=True), sg.Radio('My second Radio!', "RADIO1")],
		# 多行文本
		[sg.Multiline(default_text='This is the default Text should you decide not to type anything', size=(35, 3)),
		 sg.Multiline(default_text='A second multi-line', size=(35, 3))],
		# 下拉框
		[sg.InputCombo(('Combobox 1', 'Combobox 2'), size=(20, 3))],
		# 水平 拖动条
		[sg.Slider(range=(1, 100), orientation='h', size=(34, 20), default_value=85)],
		# 垂直 拖动条
		[sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=25),
		 sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=75),
		 sg.Slider(range=(1, 100), orientation='v', size=(5, 20), default_value=10)],
		# 列表框
		[sg.Listbox(values=('Listbox 1', 'Listbox 2', 'Listbox 3'), size=(30, 3))],
		# 整列
		[sg.Column(column1, background_color='#FF3E96')],
		# 横线
		[sg.Text('_'  * 80)],
		# 文本
		[sg.Text('Choose A Folder', size=(35, 1))],
		# 文件浏览器
		[sg.Text('Your Folder', size=(15, 1), auto_size_text=False, justification='right'),
		 sg.InputText('Default Folder'), sg.FolderBrowse()],

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

		[sg.Button('ok'), sg.Button('cancel')],

	]


	# Create the Window=================================2
	window = sg.Window('window titel', layout=layout, default_element_size=(40,1))

	# Event Loop to process "events" and get the "values" of the inputs
	while True:
		event, values = window.read()  # ====================3
		# if user closes window or clicks cancel
		if event in (None, 'cancel'):
			break;
		# 打印
		#
		'''
		event---按钮事件,ok 
		values 是字典,所有可输入的值
		{
			0: 'This is my text', 
			1: False, 
			2: True, 
			3: True, 
			4: False, 
			5: 'This is the dything\n', 
			6: 'A second multi-line\n', 
			7: 'Combobox 1', 
			8: 85.0, 
			9: 25.0, 
			10: 75.0, 
			11: 10.0, 
			12: [], 
			13: 'Spin Box 1', 
			14: 'Spin Box 2', 
			15: 'Spin Box 3', 
			16: 'Default Folder', 'Browse': ''
		}
		'''
		print(event, values)

		# 弹窗提示
		sg.Popup(event, values)

	window.close()


if __name__ == '__main__':
	# test_1()
	# test_2()
	# test_3()
	test_4()




 类似资料: