Reputation: 63
I'm trying to create a simple GUI program in Python that I'd use when creating new projects. I'd like a tickbox functionality for what type of project it is (python, web, etc) and then an input box for the project name (what the directory name would be).
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()
I've made the functions so that if python is selected, the new folder will contain a "start.py" file, and if web is selected, the folder will contain "script.js, styles.css, index.html".
At the moment the only way I can choose whether the file type will be python or webapp is by inputting either '1' or '2' respectively. This is just a placeholder and a checkbox functionality would be much more practical, so I'm asking for help as to how to implement this in.
Upvotes: 2
Views: 6266
Reputation: 5754
Because you're choosing one value from a list of 2 or more, what you need is a Radio Button rather than a checkbox. They work in similar ways in that they both return a bool True/False, but the Radio Button ensures only 1 of the choices is made.
Your event loop looks a little odd as well. The window.read call should be done within the while loop itself. You basically want to keep processing button presses, etc, until the user has completed the action. Here "completed" would be that a choice is made for Python or Web project and they've input a name in the name field.
I shuffled around your code blocks / functions too so that the GUI code remains together instead of the functions being right in the middle of it all making it a little difficult to see.
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()
Upvotes: 1
Reputation: 154
You Can use below elements to achieve your goal
1) sg.Frame Layout
2) Checkbox events
3) Key for dictionary based lookup for values
Also you can add further checks e.g. only maximum one checkbox is selected at any time
Here is the modified code below .
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()
Upvotes: 2