Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to trap an event #6738

Open
3 of 10 tasks
MikeGreen99 opened this issue Apr 8, 2024 · 2 comments
Open
3 of 10 tasks

[Question] How to trap an event #6738

MikeGreen99 opened this issue Apr 8, 2024 · 2 comments
Labels
Port - TK PySimpleGUI question Further information is requested

Comments

@MikeGreen99
Copy link

MikeGreen99 commented Apr 8, 2024

Type of Issue (Enhancement, Error, Bug, Question)

Question

Operating System

ubuntu 22.04

PySimpleGUI Port (tkinter, Qt, Wx, Web)

tkinter


Versions 5

Version information can be obtained by calling sg.main_get_debug_data()
Or you can print each version shown in ()

Python version (sg.sys.version)

3,12

PySimpleGUI Version (sg.__version__)

5

GUI Version (tkinter (sg.tclversion_detailed), PySide2, WxPython, Remi)

5


Your Experience In Months or Years (optional)

Years Python programming experience6 months

Years Programming experience overall40 years

Have used another Python GUI Framework? (tkinter, Qt, etc) (yes/no is fine)no

Anything else you think would be helpful?


Troubleshooting

These items may solve your problem. Please check those you've done by changing - [ ] to - [X]

  • Searched main docs for your problem PySimpleGUI Documenation
  • Looked for Demo Programs that are similar to your goal. It is recommend you use the Demo Browser! Demo Programs
  • None of your GUI code was generated by an AI algorithm like GPT
  • If not tkinter - looked for Demo Programs for specific port
  • For non tkinter - Looked at readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
  • Run your program outside of your debugger (from a command line)
  • Searched through Issues (open and closed) to see if already reported Issues.PySimpleGUI.com
  • Have upgraded to the latest release of PySimpleGUI on PyPI (lastest official version)
  • Tried running the Development Build. Your problem may have already been fixed but not released. Check Home Window for release notes and upgrading capability
  • For licensing questions please email license@PySimpleGUI.com

Detailed Description

How to Trap an event when user enters a field ?

Code To Duplicate

# Forum01.py Apr.06.2024
#  How to make an Event for the Name and Age Fields so I can validate ???
import PySimpleGUI as sg
layout = [
    [sg.Text('Name: ',justification='right',tooltip="Name 3-31 Char") ,sg.InputText(size=(31,1))],
    [sg.Text('Age:    ', justification='right', tooltip="Age 16-99"), sg.InputText(size=(3, 1))],
    [sg.Button("Update"), sg.Button("Exit")],
	]
winx = sg.Window('Simple data entry window', layout)
while True:
    event, values = winx.read() # Display Window & wait for an event
    if event == 'Exit' or event == sg.WINDOW_CLOSE_ATTEMPTED_EVENT: # Exit button or upper left X
       break
    if event == 'Name' :  # THIS DOESN'T Trap Name Entry
        print('Name Validation')
    if event == 'Age' :  # THIS DOESN'T Trap Age Entry
        print('Age Validation')
winx.close()

Screenshot, Sketch, or Drawing


Watcha Makin?

If you care to share something about your project, it would be awesome to hear what you're building. Small home project

@pysimpleissue pysimpleissue bot closed this as completed Apr 8, 2024
@pysimpleissue pysimpleissue bot added Fill issue form or you will be REJECTED You MUST use the supplied template to submit a request. PySimpleGUI Issues Bot Has Detected an Error labels Apr 8, 2024
@jason990420 jason990420 changed the title [ Enhancement/Bug/Question] NOTE - you can also call sg.main() or sg.main_open_github_issue() to post an issue [Question] How to trap an event Apr 8, 2024
@pysimpleissue pysimpleissue bot removed Fill issue form or you will be REJECTED You MUST use the supplied template to submit a request. PySimpleGUI Issues Bot Has Detected an Error labels Apr 8, 2024
@pysimpleissue pysimpleissue bot reopened this Apr 8, 2024
Repository owner deleted a comment from pysimpleissue bot Apr 8, 2024
Repository owner deleted a comment from pysimpleissue bot Apr 8, 2024
Repository owner deleted a comment from pysimpleissue bot Apr 8, 2024
Repository owner deleted a comment from pysimpleissue bot Apr 8, 2024
Repository owner deleted a comment from pysimpleissue bot Apr 8, 2024
Repository owner deleted a comment from pysimpleissue bot Apr 8, 2024
@jason990420
Copy link
Collaborator

jason990420 commented Apr 8, 2024

Some options to element or sg.Window, refer Tkinter Port Call Reference

  • With option enable_events=True in sg.Input element, then changes to this element are immediately reported as an event.
  • With option key in sg.Input element to uniquely identifies this element.
  • With option enable_close_attempted_event=True in sg.Window, then the window will not close when "X" clicked. Instead an event sg.WINDOW_CLOSE_ATTEMPTED_EVENT if returned from window.read.

Revised code

import PySimpleGUI as sg

layout = [
    [sg.Text('Name: ',justification='right',tooltip="Name 3-31 Char") ,sg.InputText(size=(31,1), enable_events=True, key="Name")],
    [sg.Text('Age:    ', justification='right', tooltip="Age 16-99"),  sg.InputText(size=(3, 1), enable_events=True, key="Age")],
    [sg.Button("Update"), sg.Button("Exit")],
    ]
winx = sg.Window('Simple data entry window', layout, enable_close_attempted_event=True)

while True:

    event, values = winx.read() # Display Window & wait for an event

    if event == 'Exit' or event == sg.WINDOW_CLOSE_ATTEMPTED_EVENT: # Exit button or upper left X
       break

    if event == 'Name' :  # THIS DOESN'T Trap Name Entry
        print('Name Validation:', values[event])

    if event == 'Age' :  # THIS DOESN'T Trap Age Entry
        print('Age Validation:', values[event])

winx.close()

@jason990420 jason990420 added question Further information is requested Port - TK PySimpleGUI labels Apr 8, 2024
@PySimpleGUI
Copy link
Owner

PySimpleGUI commented Apr 8, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Port - TK PySimpleGUI question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants