博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
神器 PySimpleGUI 初体验
阅读量:4049 次
发布时间:2019-05-25

本文共 6976 字,大约阅读时间需要 23 分钟。

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

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

pip install pysimplegui

or
pip3 install pysimplegui

或者自己下载,链接:

以下就是初次体验的例程
#!usr/bin/env python# -*-coding:utf-8 -*-# @Time      :2019/10/15 17:14# @Author    :xxx# @File      :1-体验.py# description:# @SoftWare  :PyCharm Community Editionimport PySimpleGUI as sgdef 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()

转载地址:http://linci.baihongyu.com/

你可能感兴趣的文章
JavaScript的一些基础-数据类型
查看>>
转载一个webview开车指南以及实际项目中的使用
查看>>
android中对于非属性动画的整理
查看>>
一个简单的TabLayout的使用
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>
vsftp 配置具有匿名登录也有系统用户登录,系统用户有管理权限,匿名只有下载权限。
查看>>
linux安装usb wifi接收器
查看>>
多线程使用随机函数需要注意的一点
查看>>
getpeername,getsockname
查看>>