PySimpleGUI icon indicating copy to clipboard operation
PySimpleGUI copied to clipboard

[Enhancement ] Keep graph coordinate system constant after graph element changes in size

Open srwi opened this issue 2 years ago • 6 comments

Question


Operating System

Windows 11

PySimpleGUI Port (tkinter, Qt, Wx, Web)

tkinter


Versions

Python version (sg.sys.version)

3.10.11

PySimpleGUI Version (sg.__version__)

4.60.4

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

8.6.12


Troubleshooting

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

  • [x] Searched main docs for your problem www.PySimpleGUI.org
  • [ ] Looked for Demo Programs that are similar to your goal. It is recommend you use the Demo Browser! Demos.PySimpleGUI.org
  • [x] 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)
  • [x] Run your program outside of your debugger (from a command line)
  • [x] Searched through Issues (open and closed) to see if already reported Issues.PySimpleGUI.org
  • [ ] Have upgraded to the latest release of PySimpleGUI on PyPI (lastest official version)
  • [ ] Tried using the PySimpleGUI.py file on GitHub. Your problem may have already been fixed but not released

Detailed Description

I have a graph element with expand_x=True and expand_y=True and want to keep a constant coordinate system 0 <= x,y <= 200 even after the graph size changes. I set the TopRight attribute to (200, 200) every time the window size changes (configure event). Unfortunately the coordinate system does not respect the new TopRight value and instead scales proportionally with the graph element size.

Steps to reproduce:

  1. Run code below
  2. Confirm graph size is 200x200 in console output
  3. Move mouse cursor to top right corner: Cursor coordinates will say roughly 200x200
  4. Resize window to twice the width so that the console output says roughly 400x200
  5. Move mouse cursor to top right corner: Cursor coordinates will say roughly 400x200

Expected results:

After resizing and explicitly setting TopRight = (200, 200) I would expect the top right cursor coordinates to be 200x200. Instead the top right cursor coordinates are 400x200.

Code To Duplicate


import PySimpleGUI as sg


def main():
    layout = [
        [sg.Graph(canvas_size=(200, 200),
                  graph_bottom_left=(0, 0),
                  graph_top_right=(200, 200),
                  key="-GRAPH-",
                  motion_events=True,
                  enable_events=True,
                  background_color="white",
                  expand_x=True,
                  expand_y=True,
                  drag_submits=True)],
    ]

    window = sg.Window("Window", layout, finalize=True, resizable=True)
    window.bind("<Configure>", "-CONFIGURE-")

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

        graph = window["-GRAPH-"]

        if event == "-CONFIGURE-":
            print("New graph size:", graph.get_size())
            graph.TopRight = (200, 200)
        elif event == "-GRAPH-+MOVE":
            print("Mouse position:", values["-GRAPH-"])

    window.close()


if __name__ == "__main__":
    main()

srwi avatar Jul 22 '23 19:07 srwi

graph.TopRight isn't a property.

Take a look at the call reference (https://www.pysimplegui.org/en/latest/call%20reference/#graph-element) to find valid calls and properties.

I think what you're after is change_coordinates

image

PySimpleGUI avatar Jul 22 '23 23:07 PySimpleGUI

Thank you, @PySimpleGUI, for your quick response!

I think what you're after is change_coordinates

Actually change_coordinates also just sets TopRight and BottomLeft.

After looking at the implementation of Element.set_size I realized that the Graph element requires specific setting of CanvasSize after a resize.

grafik

Probably this does not happen after implicit scaling through the expand_x=True and expand_y=True attributes. Therefore replacing graph.TopRight = (200, 200) with graph.CanvasSize = graph.get_size() has the desired effect of keeping the coordinate system [0, 200].


Is this behavior expected or can this be considered a bug? There is some inconsistency between explicitly scaling a Graph element (set_size does not show the problem) and implicit scaling (scaling as a result of expand_x=True and expand_y=True shows the problem).

srwi avatar Jul 23 '23 07:07 srwi

The expand feature was added significantly after the initial development of the Graph and all the other elements for that matter.

When expansion was added to the graph element, the addition did not include any automated scaling. The <Configure> event is not used by the Graph element to respond to resizing in an automated/intelligent way. It's assumed that the user code will perform those kinds of changes by doing what you're doing which is binding to the configure event.

Does calling set_size after you get a config event solve this problem or does that not get the right member variables set? Rather than a bug (there is an argument to be made it is a bug) I think of it perhaps as a feature not as full-featured as it should/could be. Either way, this should be marked as an enhancement or bug.

PySimpleGUI avatar Jul 23 '23 14:07 PySimpleGUI

BTW.... Thank you for the "Thanks"... they're really appreciated. image

Thank you for taking the time to track down the underlying problem. image It helps with understanding and fixing problems like this one. Your experience in programming shows.

PySimpleGUI avatar Jul 23 '23 15:07 PySimpleGUI

Does calling set_size after you get a config event solve this problem or does that not get the right member variables set?

Yes, graph.set_size(graph.get_size()) has the same effect but looks totally redundant, so I prefer to set CanvasSize directly.

I briefly tried to find a suitable place to insert said line of code in PySimpleGUI but couldn't find anything except for using the configure event. Unfortunately I don't know my way around the inner workings of PySimpleGUI well enough to be of further help here.

Rather than a bug (there is an argument to be made it is a bug) I think of it perhaps as a feature not as full-featured as it should/could be. Either way, this should be marked as an enhancement or bug.

I think enhancement suits this well! For a new user it could be helpful if the automatic expansion behaves the same as explicit resizing of a graph element. Shall I rephrase the original post or does changing the title/label suffice?

And thank you for the kind words!

srwi avatar Jul 23 '23 16:07 srwi

Marking this as an enhancement so that the Graph element will better handle the expand events. Thanks again for being a great help all the way through from logging to here. Much appreciated!

PySimpleGUI avatar Jul 24 '23 12:07 PySimpleGUI