gpt4 book ai didi

python - 如何在 PySimpleGUIQt 中实现复选框功能?

转载 作者:行者123 更新时间:2023-12-01 00:15:58 25 4
gpt4 key购买 nike

我正在尝试用 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”。 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()

关于python - 如何在 PySimpleGUIQt 中实现复选框功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59333376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com