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

[ Enhancement/Question] Programmatically setting Horizontal Scroll position of a Column() #6735

Open
3 of 9 tasks
jansabai opened this issue Apr 5, 2024 · 9 comments
Open
3 of 9 tasks
Labels
Done - Install Dev Build (see docs for how) See https://docs.pysimplegui.com/en/latest/documentation/installing_licensing/upgrading/ enhancement New feature or request Port - TK PySimpleGUI

Comments

@jansabai
Copy link

jansabai commented Apr 5, 2024

Type of Issue (Enhancement, Question)

Programmatically setting Horizontal Scroll position of a Column

Operating System

PC Windows 11 / macOS Sonoma

PySimpleGUI Port (tkinter, Qt, Wx, Web)

tkinter


Versions

Python version: 3.12.0
Platform: PC (Windows 11) and Mac (macOS Sonoma)
Port: PySimpleGUI
tkinter version: 8.6.13
PySimpleGUI version: 4.61.0.201

Python version (3.12.0)

PySimpleGUI Version (4.61.0.201)

GUI Version (tkinter 8.6.13)


Your Experience In Months or Years (optional) :

Years Python programming experience : 10 years

Years Programming experience overall : 40 years

Have used another Python GUI Framework? (tkinter, Qt, etc) YES


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
  • [X ] 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

I am writing an application that uses a spreadsheet-kind-of input area.
All input cells are placed in rows and columns in a layout that is put in a SCROLLABLE COLUMN (horizontal and vertical) because the input sheet might be larger than the displayed window.

It is important to "follow" the focussed field during input and keep it visible in the scrollable column.
Vertically, this can be done programmatically by setting and controlling the vertical scroll position of the column with:

window['<scrollable_column_key>'].set_vscroll_position(percentage)

Sadly. There is no such feature to control the horizontal scrolling position of the scrollable column...
window['<scrollable_column_key>'].set_hscroll_position(percentage)

When running the code, the following error is generated:

window['<input_sheet>'].set_hscroll_position(scroll_percentage)
^^^^^^^^^^^^^^^
AttributeError: Object has no attribute 'set_hscroll_position'. Did you mean: 'set_vscroll_position'?

It is obvious that the "set_hscroll_position" attribute is not available.

But in that case, how does one keep the focussed fields in the visible part of the scrollable column?
Without being able to do that, the input becomes really awkward and unhandy to use...

Any suggestions on a workaround or on how to fix the problem?

Thanks!

@jason990420
Copy link
Collaborator

jason990420 commented Apr 5, 2024

Simple demo code for horizontal-position scroll.

import PySimpleGUI as sg

def set_hscroll_position(self, percent_from_left):
    """
    Attempts to set the horizontal scroll postition for an element's Widget
    :param percent_from_left: From 0 to 1.0, the percentage from the left to move scrollbar to
    :type percent_from_left:  (float)
    """
    if self.Type == sg.ELEM_TYPE_COLUMN and self.Scrollable:
        widget = self.widget.canvas  # scrollable column is a special case
    else:
        widget = self.widget

    try:
        widget.xview_moveto(percent_from_left)
    except Exception as e:
        print('Warning setting the horizontal scroll (xview_moveto failed)')
        print(e)

sg.Element.set_hscroll_position = set_hscroll_position

sg.theme("DarkBlue")
sg.set_options(font=("Courier New", 20))
column_layout = [[sg.Input(f"({row}, {col})", size=6, pad=(0, 0), key=("-IN-", row, col)) for col in range(10)] for row in range(10)]
layout = [
    [sg.Button("<"),
     sg.Column(column_layout, pad=(0, 0), scrollable=True, size_subsample_width=2, size_subsample_height=2, key="-COLUMN-"),
     sg.Button(">")],
]
window = sg.Window("Title", layout, finalize=True)
percent = 0

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event == "<" and percent > 0:
        percent -= 0.1
        window["-COLUMN-"].set_hscroll_position(percent)
    elif event == ">" and percent < 1.0:
        percent += 0.1
        window["-COLUMN-"].set_hscroll_position(percent)

window.close()

image

@jansabai
Copy link
Author

jansabai commented Apr 5, 2024

WOW
Such a swift reply!
You are a champ, Jason!

Will try this immediately!!!
Thanks...

And quick question...
I registered as a private user... But my application gets traction, and on top of that, I want to support you guys!!!
HOW DO I CONVERT TO A COMMERCIAL LICENSE?

@jansabai
Copy link
Author

jansabai commented Apr 5, 2024

And Jason...

IT WORKS !!!
WOW
Super impressed...

@jansabai
Copy link
Author

jansabai commented Apr 5, 2024

Just quickly.
Problem solved for my app.
Guess, seen the code is so simple, it would be good/nice to implement it as an attribute for the column-Widget anyway...

Also, but not important...

def set_hscroll_position(self, percent_from_top):
...that should be "percent_from_left"...

Cheers!

@jansabai
Copy link
Author

jansabai commented Apr 5, 2024

Regarding converting the license to COMMERCIAL, maybe just put a button "UPGRADE" on the License Page.
Simple!

Feel free to close this thread.
ISSUE RESOLVED!

Thanks again.

@PySimpleGUI
Copy link
Owner

PySimpleGUI commented Apr 6, 2024

Yesterday I added this feature to 5.0.4.3. Jason and I used the same name set_hscroll_position, except that I used parameter named percent_from_left.

This is the docstring you'll find in the code I checked in.

    def set_hscroll_position(self, percent_from_left):
        """
        Attempts to set the horizontal scroll postition for an element's Widget
        :param percent_from_left: From 0 to 1.0, the percentage from the left to move scrollbar to
        :type percent_from_left:  (float)
        """

This is the test harness I was using to test it:

import PySimpleGUI as sg

col_layout = [[sg.Text('Column')],
            [sg.Multiline(size=(150,40))],
            [sg.Input(key='-IN-')],
            [sg.Text('Text at the bottom')],

            ]

layout = [[sg.Column(col_layout, k='-COL-', scrollable=True, size=(400,400))],
          [sg.VPush()],
          [sg.Text('% to set scroll from left'), sg.In(k='-RIGHT-', s=4), sg.B('Scroll'), sg.Button('Exit'), sg.Sizegrip()]
          ]

window = sg.Window('Scroll Test', layout, resizable=True)

while True:
    event, values = window.read()
    print(event, values)
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    if event == 'Scroll':
        window['-COL-'].set_hscroll_position(float(values['-RIGHT-'])/100)

window.close()

I forgot to come back to this issue and post that I made the change. I'm sorry about that! I got really busy.

@PySimpleGUI PySimpleGUI added enhancement New feature or request Done - Install Dev Build (see docs for how) See https://docs.pysimplegui.com/en/latest/documentation/installing_licensing/upgrading/ Port - TK PySimpleGUI labels Apr 6, 2024
@PySimpleGUI
Copy link
Owner

Regarding converting the license to COMMERCIAL, maybe just put a button "UPGRADE" on the License Page.

We are working on this. In the meantime we're writing a FAQ entry on how to go about changing the license.

Thank you for the support!

@jansabai
Copy link
Author

jansabai commented Apr 6, 2024 via email

@jansabai
Copy link
Author

jansabai commented Apr 6, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Done - Install Dev Build (see docs for how) See https://docs.pysimplegui.com/en/latest/documentation/installing_licensing/upgrading/ enhancement New feature or request Port - TK PySimpleGUI
Projects
None yet
Development

No branches or pull requests

3 participants