如何在 PySimpleGUIQt 中实现复选框功能?
How Do I Implement Checkbox Functionality in PySimpleGUIQt?
我正在尝试在 Python 中创建一个简单的 GUI 程序,我会在创建新项目时使用它。我想要一个项目类型的复选框功能(python、web 等),然后是项目名称的输入框(目录名称是什么)。
import os
import PySimpleGUIQt as sg
sg.change_look_and_feel('DarkAmber') #colour
#layout of window
layout = [
[sg.Text('File Types')],
[sg.Text('1. Python file (start.py)')],
[sg.Text('2. Web app (script.js, index.html, styles.css)')],
[sg.Text('Choose your file type (1 or 2):'), sg.InputText()],
[sg.Text('Project Name:'), sg.InputText()],
[sg.Button('Ok'), sg.Button('Cancel')],
]
window = sg.Window('Project Creator', layout) #make the window
event, values = window.read()
ProjectName = values[1]
def make_file_python(ProjectName): #function to make python project
os.makedirs('../' + ProjectName)
open(f"..\{ProjectName}\start.py", 'x')
def make_file_webapp(ProjectName): #function to make webapp project
os.makedirs('../' + ProjectName)
open(f"..\{ProjectName}\index.html", 'x')
open(f"..\{ProjectName}\style.css", 'x')
open(f"..\{ProjectName}\script.js", 'x')
count = 0
while count < 1:
if event in (None, 'Cancel'):
break
elif values[0] == '1':
make_file_python(ProjectName)
count +=1
elif values[0] == '2':
make_file_webapp(ProjectName)
count +=1
elif count >= 1:
break
window.close()
我已经做了这些功能,如果选择 python,新文件夹将包含一个 "start.py" 文件,如果选择 web,该文件夹将包含 "script.js, styles.css, index.html" .
目前,我可以选择文件类型是 python 还是 webapp 的唯一方法是分别输入“1”或“2”。这只是一个占位符,复选框功能会更实用,所以我正在寻求有关如何实现它的帮助。
您可以使用以下要素来实现您的目标
1) sg.Frame 布局
2) 复选框事件
3) 基于字典查找值的键
您还可以添加进一步的检查,例如任何时候最多只能选中一个复选框
下面是修改后的代码。
import os
import PySimpleGUIQt as sg
sg.change_look_and_feel('DarkAmber') #colour
#layout of window
layout = [
[sg.Frame(layout=[
[sg.Checkbox('1. Python file (start.py)', default=False,key='pyfile'),
sg.Checkbox('2. Web app (script.js, index.html, styles.css)',
default=False,key='webapp')]],
title='Select File Type from the Checkbox',title_color='red',
relief=sg.RELIEF_SUNKEN, tooltip='Use these to set flags')],
[sg.Text('Project Name:'), sg.InputText()],
[sg.Submit(), sg.Button('Cancel')],
]
window = sg.Window('Project Creator', layout) #make the window
event, values = window.read()
ProjectName = values[0]
def make_file_python(ProjectName): #function to make python project
os.makedirs('../' + ProjectName)
open(f"..\{ProjectName}\start.py", 'x')
def make_file_webapp(ProjectName): #function to make webapp project
os.makedirs('../' + ProjectName)
open(f"..\{ProjectName}\index.html", 'x')
open(f"..\{ProjectName}\style.css", 'x')
open(f"..\{ProjectName}\script.js", 'x')
count = 0
while count < 1:
if event in (None, 'Cancel'):
break
elif values['pyfile'] == True:
make_file_python(ProjectName)
count +=1
elif values['webapp'] == True:
make_file_webapp(ProjectName)
count +=1
elif count >= 1:
break
window.close()
因为您要从 2 个或更多值的列表中选择一个值,所以您需要的是单选按钮而不是复选框。它们的工作方式相似,因为它们都是 return 布尔值 True/False,但单选按钮确保只做出其中一个选择。
您的事件循环看起来也有点奇怪。 window.read 调用应该在 while 循环本身内完成。您基本上希望继续处理按钮按下等操作,直到用户完成操作。这里 "completed" 将是为 Python 或 Web 项目做出选择,并且他们在名称字段中输入了名称。
我也对你的代码块/函数进行了调整,以便 GUI 代码保持在一起,而不是函数就在它的中间,这使得它有点难以看清。
import os
import PySimpleGUIQt as sg
def make_file_python(ProjectName): #function to make python project
os.makedirs('../' + ProjectName)
open(f"..\{ProjectName}\start.py", 'x')
def make_file_webapp(ProjectName): #function to make webapp project
os.makedirs('../' + ProjectName)
open(f"..\{ProjectName}\index.html", 'x')
open(f"..\{ProjectName}\style.css", 'x')
open(f"..\{ProjectName}\script.js", 'x')
sg.change_look_and_feel('DarkAmber') #colour
#layout of window
layout = [
[sg.Text('File Types')],
[sg.Radio('Python file (start.py)', 1, key='-PY-')],
[sg.Radio('Web app (script.js, index.html, styles.css)', 1, key='-WEB-')],
[sg.Text('Project Name:'), sg.InputText(key='-NAME-')],
[sg.Button('Ok'), sg.Button('Cancel')],
]
window = sg.Window('Project Creator', layout) #make the window
while True:
event, values = window.read()
ProjectName = values['-NAME-']
if event in (None, 'Cancel'):
break
if values['-PY-'] and ProjectName:
make_file_python(ProjectName)
break
elif values['-WEB-'] and ProjectName:
make_file_webapp(ProjectName)
break
window.close()
我正在尝试在 Python 中创建一个简单的 GUI 程序,我会在创建新项目时使用它。我想要一个项目类型的复选框功能(python、web 等),然后是项目名称的输入框(目录名称是什么)。
import os
import PySimpleGUIQt as sg
sg.change_look_and_feel('DarkAmber') #colour
#layout of window
layout = [
[sg.Text('File Types')],
[sg.Text('1. Python file (start.py)')],
[sg.Text('2. Web app (script.js, index.html, styles.css)')],
[sg.Text('Choose your file type (1 or 2):'), sg.InputText()],
[sg.Text('Project Name:'), sg.InputText()],
[sg.Button('Ok'), sg.Button('Cancel')],
]
window = sg.Window('Project Creator', layout) #make the window
event, values = window.read()
ProjectName = values[1]
def make_file_python(ProjectName): #function to make python project
os.makedirs('../' + ProjectName)
open(f"..\{ProjectName}\start.py", 'x')
def make_file_webapp(ProjectName): #function to make webapp project
os.makedirs('../' + ProjectName)
open(f"..\{ProjectName}\index.html", 'x')
open(f"..\{ProjectName}\style.css", 'x')
open(f"..\{ProjectName}\script.js", 'x')
count = 0
while count < 1:
if event in (None, 'Cancel'):
break
elif values[0] == '1':
make_file_python(ProjectName)
count +=1
elif values[0] == '2':
make_file_webapp(ProjectName)
count +=1
elif count >= 1:
break
window.close()
我已经做了这些功能,如果选择 python,新文件夹将包含一个 "start.py" 文件,如果选择 web,该文件夹将包含 "script.js, styles.css, index.html" .
目前,我可以选择文件类型是 python 还是 webapp 的唯一方法是分别输入“1”或“2”。这只是一个占位符,复选框功能会更实用,所以我正在寻求有关如何实现它的帮助。
您可以使用以下要素来实现您的目标
1) sg.Frame 布局
2) 复选框事件
3) 基于字典查找值的键
您还可以添加进一步的检查,例如任何时候最多只能选中一个复选框
下面是修改后的代码。
import os
import PySimpleGUIQt as sg
sg.change_look_and_feel('DarkAmber') #colour
#layout of window
layout = [
[sg.Frame(layout=[
[sg.Checkbox('1. Python file (start.py)', default=False,key='pyfile'),
sg.Checkbox('2. Web app (script.js, index.html, styles.css)',
default=False,key='webapp')]],
title='Select File Type from the Checkbox',title_color='red',
relief=sg.RELIEF_SUNKEN, tooltip='Use these to set flags')],
[sg.Text('Project Name:'), sg.InputText()],
[sg.Submit(), sg.Button('Cancel')],
]
window = sg.Window('Project Creator', layout) #make the window
event, values = window.read()
ProjectName = values[0]
def make_file_python(ProjectName): #function to make python project
os.makedirs('../' + ProjectName)
open(f"..\{ProjectName}\start.py", 'x')
def make_file_webapp(ProjectName): #function to make webapp project
os.makedirs('../' + ProjectName)
open(f"..\{ProjectName}\index.html", 'x')
open(f"..\{ProjectName}\style.css", 'x')
open(f"..\{ProjectName}\script.js", 'x')
count = 0
while count < 1:
if event in (None, 'Cancel'):
break
elif values['pyfile'] == True:
make_file_python(ProjectName)
count +=1
elif values['webapp'] == True:
make_file_webapp(ProjectName)
count +=1
elif count >= 1:
break
window.close()
因为您要从 2 个或更多值的列表中选择一个值,所以您需要的是单选按钮而不是复选框。它们的工作方式相似,因为它们都是 return 布尔值 True/False,但单选按钮确保只做出其中一个选择。
您的事件循环看起来也有点奇怪。 window.read 调用应该在 while 循环本身内完成。您基本上希望继续处理按钮按下等操作,直到用户完成操作。这里 "completed" 将是为 Python 或 Web 项目做出选择,并且他们在名称字段中输入了名称。
我也对你的代码块/函数进行了调整,以便 GUI 代码保持在一起,而不是函数就在它的中间,这使得它有点难以看清。
import os
import PySimpleGUIQt as sg
def make_file_python(ProjectName): #function to make python project
os.makedirs('../' + ProjectName)
open(f"..\{ProjectName}\start.py", 'x')
def make_file_webapp(ProjectName): #function to make webapp project
os.makedirs('../' + ProjectName)
open(f"..\{ProjectName}\index.html", 'x')
open(f"..\{ProjectName}\style.css", 'x')
open(f"..\{ProjectName}\script.js", 'x')
sg.change_look_and_feel('DarkAmber') #colour
#layout of window
layout = [
[sg.Text('File Types')],
[sg.Radio('Python file (start.py)', 1, key='-PY-')],
[sg.Radio('Web app (script.js, index.html, styles.css)', 1, key='-WEB-')],
[sg.Text('Project Name:'), sg.InputText(key='-NAME-')],
[sg.Button('Ok'), sg.Button('Cancel')],
]
window = sg.Window('Project Creator', layout) #make the window
while True:
event, values = window.read()
ProjectName = values['-NAME-']
if event in (None, 'Cancel'):
break
if values['-PY-'] and ProjectName:
make_file_python(ProjectName)
break
elif values['-WEB-'] and ProjectName:
make_file_webapp(ProjectName)
break
window.close()