PySimpleGUI 2020: Exploring Button Targets – File, Calendar, and Color Chooser Buttons (Part 7)

Posted by


In this tutorial, we will be discussing how to use button targets in PySimpleGUI. Button targets allow you to specify different functionalities for different buttons within your GUI. We will cover three types of button targets in this tutorial – File button, Calendar button, and Color Chooser button.

  1. File Button
    The File button target allows you to create a button that opens a file dialog box when clicked. This is a useful feature when you want the user to select a file or a directory from their system. To create a File button in PySimpleGUI, you can use the file_types parameter to specify the types of files the user can select.

Here is an example code snippet that demonstrates how to create a File button in PySimpleGUI:

import PySimpleGUI as sg

layout = [
    [sg.Text('Select a file: '), sg.InputText(key='-FILE-'), sg.FileBrowse()],
    [sg.Button('Submit')]
]

window = sg.Window('File Button Example', layout)

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Submit':
        break

window.close()

In this code snippet, we are creating a window with a Text element, an InputText element, a FileBrowse button, and a Submit button. When the user clicks on the FileBrowse button, a file dialog box will open, and the selected file path will be displayed in the InputText element.

  1. Calendar Button
    The Calendar button target allows you to create a button that opens a calendar dialog box when clicked. This is useful when you want the user to select a date or a range of dates. To create a Calendar button in PySimpleGUI, you can use the calendar_popup function.

Here is an example code snippet that demonstrates how to create a Calendar button in PySimpleGUI:

import PySimpleGUI as sg

layout = [
    [sg.Button('Select Date')],
    [sg.Output(size=(20, 1))]
]

window = sg.Window('Calendar Button Example', layout)

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    if event == 'Select Date':
        date = sg.popup_get_date()
        print(date)

window.close()

In this code snippet, we are creating a window with a Select Date button and an Output element. When the user clicks on the Select Date button, a calendar dialog box will open, and the selected date will be displayed in the Output element.

  1. Color Chooser Button
    The Color Chooser button target allows you to create a button that opens a color chooser dialog box when clicked. This is useful when you want the user to select a color for a specific element in your GUI. To create a Color Chooser button in PySimpleGUI, you can use the color_chooser function.

Here is an example code snippet that demonstrates how to create a Color Chooser button in PySimpleGUI:

import PySimpleGUI as sg

layout = [
    [sg.Text('Select a color: '), sg.InputText(key='-COLOR-'), sg.Button('Select Color')],
    [sg.Button('Submit')]
]

window = sg.Window('Color Chooser Button Example', layout)

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Submit':
        break
    if event == 'Select Color':
        color = sg.popup_get_color()
        window['-COLOR-'].update(color)

window.close()

In this code snippet, we are creating a window with a Text element, an InputText element, a Select Color button, and a Submit button. When the user clicks on the Select Color button, a color chooser dialog box will open, and the selected color will be displayed in the InputText element.

In this tutorial, we have covered three types of button targets in PySimpleGUI – File button, Calendar button, and Color Chooser button. You can use these button targets to enhance the functionality of your GUI and provide a better user experience. I hope you found this tutorial helpful! Thank you for reading.

0 0 votes
Article Rating

Leave a Reply

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@jean-polbrasseur9755
3 hours ago

Merci pour votre travail, les vidéos sont très instructives sur la manière de procéder. PySimpleGUI est le premier qui me donne vraiment envie de continuer
à progresser et d'en apprendre plus sur le sujet. Thank you.

@ut7746
3 hours ago

Every informative video
I have some questions:
Is there a documentation of how to argument the way the date format is outputed? example: I can set it to either out put 07/08/2001 or 2001-12-11
and
is it possible to have the date button on react to mouse hover over them so you can see what you are going to choose?

@GeorgeTrialonis
3 hours ago

Thanks for the videos; they are very instructive. I have a question: Is it possible to change the color of the little clickable numbers (not all of them) that represent days on the calendar? Thank you.

@triangulatorr4559
3 hours ago

Great series of videos. Clearly I'm 7 in. Moron question – why have an invisible input followed by a browse rather than key the browse? Basic question, I'm sure.

4
0
Would love your thoughts, please comment.x
()
x