Python Forum
[PySimpleGUI] Tutorial - Name, Address, Phone GUI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PySimpleGUI] Tutorial - Name, Address, Phone GUI
#21
After the form shows up, I make a Combobox selection and press Submit button, an error comes out as listed below. (Although the followed MsgBox has correct content and can be closed normally)

C:\Works\Python\PySimpleGUI-master>py
Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import Demo_Recipes
>>> Demo_Recipes.Everything()
can't invoke "winfo" command: application has been destroyed
while executing
"winfo exists $w"
(procedure "ttk::entry::AutoScroll" line 3)
invoked from within
"ttk::entry::AutoScroll .42053744.42054352"
(in namespace inscope "::" script line 1)
invoked from within
"::namespace inscope :: {ttk::entry::AutoScroll .42053744.42054352}"
("uplevel" body line 1)
invoked from within
"uplevel #0 $Repeat(script)"
(procedure "ttk::Repeat" line 3)
invoked from within
"ttk::Repeat"
("after" script)
>>>
Reply
#22
I had noticed an error prone problem in the SG design. The SG use list as the eturned "values" which can only be accessed by the index. This makes it hard to figure out which item in the list belongs to which element on the form, and makes it worse when form's layout was modified later.

Dictionary might be better than list for this purpose where item can be accessed through the key. The problem now becomes where is the key comes from?

Maybe a "name" option is the answer. Each kind of elements which will be collected into the "values" should have a "name" option. This name was used as the key. If user forget to set it (default is None), this element will be excluded from the "values" and causes an exception when accessed. User can easily notice this error and correct it during testing.
Reply
#23
Wow, that's just plain weird. I wasn't able to duplicate this.
The error messages....
They happens AFTER you press submit? Only if you make a different selection with combobox than the default?
Can you check what version of PySimpleGUI you have installed by typing
pip list

(Aug-07-2018, 10:23 AM)jfong Wrote: I had noticed an error prone problem in the SG design. The SG use list as the eturned "values" which can only be accessed by the index. This makes it hard to figure out which item in the list belongs to which element on the form, and makes it worse when form's layout was modified later.

The thinking on return values is as follows:
The caller of the GUI is assumed to be the designer. If the form is updated so that more or less input values are returned, then the assumption is that the caller knows to update those return values. The return values and the form need to be kept in sync.

It's relatively easy to edit both the return values and the form.

I suppose it's possible to add a layer that will convert the return values into a dictionary. I've seen this done already in some of the code others have written.

I want to stay away from dictionaries in the code at this time. PySimpleGUI's charter is to be 'simple' for everyone to use. Many new programmers that potentially want a GUI won't have been educated on dictionaries. The "beauty" of the return values mechanism is that it's very simple to understand and write code for.

MODERATORS
Can you tell me what I need to do in order to get listed in the TUTORIALS section of the forum? I see that WxPython is one of the packages shown. It's an installed package just like PySimpleGUI.

The goal with the post is to get users another GUI option than writing pages of code.
Reply
#24
(Aug-07-2018, 10:25 AM)PySimpleGUI Wrote: Wow, that's just plain weird. I wasn't able to duplicate this.
The error messages....
They happens AFTER you press submit? Only if you make a different selection with combobox than the default?
Can you check what version of PySimpleGUI you have installed by typing
pip list
It happens just two steps away from the form shows up. First, making a selection at the ComboBox. Second, click Submit button.

The PySimpleGUI version is 2.7.0

Quote:The thinking on return values is as follows:
The caller of the GUI is assumed to be the designer. If the form is updated so that more or less input values are returned, then the assumption is that the caller knows to update those return values. The return values and the form need to be kept in sync.

It's relatively easy to edit both the return values and the form.
It's easy to edit the form, but values is not in our hand. The user has to modify every index number influenced by the form change. I emphasize the index "number" which can only be gotton by counting. It's very difficult to find out the error cuased by a wrong index number.
Reply
#25
(Aug-08-2018, 01:17 AM)jfong Wrote: It happens just two steps away from the form shows up. First, making a selection at the ComboBox. Second, click Submit button.
Does it happen when you run the demo program from the command line, or only when you import it?


This
python Demo_Recipes.py
produces the same result?


When I tried to duplicate the steps on my machine, I get no problems. I tried it on Raspberry Pi running 3.4 and didn't get an error either.

Quote:Python 3.6.2 |Anaconda, Inc.| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import Demo_Recipes
>>> Demo_Recipes.Everything()
>>>

On unpacking return values.... I recommend not using any kind of "indexes". For example, unpack directly into the user's variables:

button, (name, address, phone) = form.LayoutAndRead(layout)
Or do the unpack in 2 steps:
button, values = form.LayoutAndRead(layout)
name, address, phone = values
I do not recommend keeping track of offsets like this:
button, values = form.LayoutAndRead(layout)

name = values[0]
address = values[1]
phone = values[2]
Reply
#26
You didn't get my point. You can't give meaningful variable name in unpack:

name, address, phone = values

unless you already knew which element each item in values represents, and you can get this information only through counting. Also this unpack statement may need to be modified each time the form was modified.

If values is a dictionary, you can write (suppose 'address' is a key):

print(values['address'])

No counting is required and no changes from now on.
Reply
#27
(Aug-08-2018, 11:12 AM)jfong Wrote: If values is a dictionary, you can write (suppose 'address' is a key):

print(values['address'])

I think I understood your point. I was trying to mitigate with existing capabilities.

How would you suggest that the 'address' dictionary entry be created? How does the user tell PySimpleGUI that the second field is 'address'?

Something like this?
g.InputText(key='address')
That adds a bit more code to every form.

But, perhaps I could do a hybrid approach where BOTH types of return values are available. The "simplified" way and the more elegant way.

Every input field would have a new keyword, "key" that can be added if desired. That will enable that field to be addressed in the return values as a dictionary lookup. I'll add a new flag already to the "read form" calls so that the return values are either a list or a dictionary.

Thanks for the suggestions... keep 'em coming!

How does something like this look to you?
import PySimpleGUI as sg

form = sg.FlexForm('Simple data entry form', use_dictionary=True)  # begin with a blank form

layout = [
          [sg.Text('Please enter your Name, Address, Phone')],
          [sg.Text('Name', size=(15, 1)), sg.InputText('1', key='name')],
          [sg.Text('Address', size=(15, 1)), sg.InputText('2', key='address')],
          [sg.Text('Phone', size=(15, 1)), sg.InputText('3', key='phone')],
          [sg.Submit(), sg.Cancel()]
         ]

button, values = form.LayoutAndRead(layout)

sg.MsgBox(button, values['name'], values['address'], values['phone'])

print(values)
The code is pretty much done.
You set the indicator that you're working with dictionary return values when you create the form. Keys for each input field are set when the form is laid out as shown in the sample.

If it looks good, I'll check the code in shortly for you to try.

DONE!

Return values as a dictionary feature officially done and posted to GitHub. It'll be part of release 2.8 that I'll post to PyPI after everything has been tested more. Afraid of breaking something at the moment.

There is a new demo, Demo_Dictionary.py, that contains above code as a template.
Reply
#28
(Aug-08-2018, 02:08 PM)PySimpleGUI Wrote: How does something like this look to you?
import PySimpleGUI as sg

form = sg.FlexForm('Simple data entry form', use_dictionary=True)  # begin with a blank form

layout = [
          [sg.Text('Please enter your Name, Address, Phone')],
          [sg.Text('Name', size=(15, 1)), sg.InputText('1', key='name')],
          [sg.Text('Address', size=(15, 1)), sg.InputText('2', key='address')],
          [sg.Text('Phone', size=(15, 1)), sg.InputText('3', key='phone')],
          [sg.Submit(), sg.Cancel()]
         ]

button, values = form.LayoutAndRead(layout)

sg.MsgBox(button, values['name'], values['address'], values['phone'])

print(values)

that gives me an error

Output:
Exception ignored in: <bound method FlexForm.__del__ of <PySimpleGUI.PySimpleGUI.FlexForm object at 0x7f13b17620f0>> Traceback (most recent call last): File "/home/brian/.local/lib/python3.5/site-packages/PySimpleGUI/PySimpleGUI.py", line 985, in __del__ for row in self.Rows: AttributeError: 'FlexForm' object has no attribute 'Rows' Traceback (most recent call last): File "/tmp/tmp.py", line 10, in <module> form = sg.FlexForm('Simple data entry form', use_dictionary=True) # begin with a blank form TypeError: __init__() got an unexpected keyword argument 'use_dictionary'
PySimpleGUI was installed with pip
Reply
#29
(Aug-08-2018, 06:39 PM)Axel_Erfurt Wrote:
(Aug-08-2018, 02:08 PM)PySimpleGUI Wrote: How does something like this look to you?
 import PySimpleGUI as sg form = sg.FlexForm('Simple data entry form', use_dictionary=True) # begin with a blank form layout = [ [sg.Text('Please enter your Name, Address, Phone')], [sg.Text('Name', size=(15, 1)), sg.InputText('1', key='name')], [sg.Text('Address', size=(15, 1)), sg.InputText('2', key='address')], [sg.Text('Phone', size=(15, 1)), sg.InputText('3', key='phone')], [sg.Submit(), sg.Cancel()] ] button, values = form.LayoutAndRead(layout) sg.MsgBox(button, values['name'], values['address'], values['phone']) print(values)
that gives me an error
Output:
Exception ignored in: <bound method FlexForm.__del__ of <PySimpleGUI.PySimpleGUI.FlexForm object at 0x7f13b17620f0>> Traceback (most recent call last): File "/home/brian/.local/lib/python3.5/site-packages/PySimpleGUI/PySimpleGUI.py", line 985, in __del__ for row in self.Rows: AttributeError: 'FlexForm' object has no attribute 'Rows' Traceback (most recent call last): File "/tmp/tmp.py", line 10, in <module> form = sg.FlexForm('Simple data entry form', use_dictionary=True) # begin with a blank form TypeError: __init__() got an unexpected keyword argument 'use_dictionary'
PySimpleGUI was installed with pip

Ah, the dictionary code only works with the current GitHub post. I have not yet released it to PyPI. The code is too new.

The easiest thing to do is to download the PySimpleGUI.py file from GitHub and put it in the same folder as your application.
Reply
#30
(Aug-08-2018, 01:42 AM)PySimpleGUI Wrote:
(Aug-08-2018, 01:17 AM)jfong Wrote: It happens just two steps away from the form shows up. First, making a selection at the ComboBox. Second, click Submit button.
Does it happen when you run the demo program from the command line, or only when you import it?

Both have the same error.

I had try it on three different system, Win8.1, Win7 and Vista, using Python v3.4.4. All has the same error. But it's fine when using Python v3.6.3 on the Win7. So it seems to me that this problem is of Python:-)

(Aug-08-2018, 02:08 PM)PySimpleGUI Wrote: The code is pretty much done.
You set the indicator that you're working with dictionary return values when you create the form. Keys for each input field are set when the form is laid out as shown in the sample.

If it looks good, I'll check the code in shortly for you to try.

DONE!

WOW, it's fast. Nice work!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020