Pysimplegui: [ Enhancement/Question] Popup justification?

Created on 23 Jul 2020  路  8Comments  路  Source: PySimpleGUI/PySimpleGUI

Type of Issues (Enhancement, Question)

Question (and possible enhancement?)

Operating System

Ubuntu 18.04.4 LTS

Python version

Python 3.6.9

PySimpleGUI Port and Version


4.24.0 Released 3-Jul-2020

Your Experience Levels In Months or Years

Python programming experience: 4 y.
Programming experience overall: 7 y.
Have used another Python GUI Framework (tkinter, Qt, etc) previously (yes/no is fine)? no.

You have completed these steps:

  • [x] Read instructions on how to file an Issue
  • [x] Searched through main docs http://www.PySimpleGUI.org for your problem
  • [x] Searched through the readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
  • [x] Looked for Demo Programs that are similar to your goal http://www.PySimpleGUI.com
  • [x] Note that there are also Demo Programs under each port on GitHub
  • [x] Run your program outside of your debugger (from a command line)
  • [x] Searched through Issues (open and closed) to see if already reported
  • [x] Try again by upgrading your PySimpleGUI.py file to use the current one on GitHub. Your problem may have already been fixed but is not yet on PyPI.

Description of Problem / Question / Details

Is it possible to change Popup element justification? I have checked the source code, but
the window that is created can't be modified (because only button is returned). May I add a pull request adding such functionality? I think changing element_justification in Popup could be a QoL improvement, that way you just need to call Popup.

All 8 comments

I just did a rare update to popups. Two in fact. One for modal and the other to add an image.

It is so easy for users to create their own popups with a single line of code, no function required, that I rarely make changes. I've thought a bit about justification. Were you thinking for the entire contents? As in all elements centered, right justified, etc? Or just the text? What did you have in mind?

The PySimpleGUI project doesn't take pull requests.

Yes, I wanted all elements centered. My plan were something like an "about" button that shows info related to version, author, license etc.
Right know, I have created a window that hide/unhide when a button is pressed. It has 5 text elements and the window has element_justification='centered'.
Before that, I had sg.Popup('....').

Try writing it as a 1-line one-shot window.
As stated in the docs, when you're at a point that you're tempted to file an issue to make a change to popup, that's a good time to make yourself a window.

So, make one and then show me how you would modify the popup API call to create that widow you're after.

You'll find a number of these 1-liner windows all over the demos, Cookbook, etc. Here's what I mean by one. It's the example from the Cookbook:

import PySimpleGUI as sg

event, values = sg.Window('Login Window',
                  [[sg.T('Enter your Login ID'), sg.In(key='-ID-')],
                  [sg.B('OK'), sg.B('Cancel') ]]).read(close=True)

Thank you very much. I'll follow your advise and use a window instead of a popup, that way I can use directly the window API.

Window example

sg.Window('About',
                  [
                      [sg.T(ABOUT_NAME), sg.T(ABOUT_VERSION)],
                      [sg.T(ABOUT_AUTHOR)],
                      [sg.T(ABOUT_DEPARTMENT)],
                      [sg.T(ABOUT_ACKNOWLEDGEMENT)],
                      [sg.T(ABOUT_LICENSE)],
                      [sg.T(ABOUT_YEAR)],
                      [sg.OK()]
                  ],
                  element_justification=CENTER,
                  font=FONT
).read(close=True)

Popup API possible modification

def Popup(*args, title=None, ..., element_justification='left'):
    ...
    window = Window(..., element_justification=element_justification)
    ...

sg.Popup(ABOUT_NAME,
         ABOUT_VERSION,
         ABOUT_AUTHOR,
         ABOUT_DEPARTMENT,
         ABOUT_ACKNOWLEDGEMENT,
         ABOUT_LICENSE,
         ABOUT_YEAR,
         font=FONT,
         element_justification='center')

I asked for it, because is more "natural" to have a centered text+button in a popup?

Anyway, I will stick with the window, thank you very much. Doing GUIs with PySimpleGUI is amazingly easy and works perfectly!

I normally don't entertain changes to popups. It's super rare, but I've been thinking about this one too as I was making this last set of changes. The question began to come up for me as to what I would want centered. If there's an image, maybe only that I want to center. Soon I found parameter creep where the popup starts to try and expose many combinations of desired outcomes.

I'm still pondering, but glad you've found a solution you're happy with for the time being. I appreciate you taking the time to post your idea, the code you created, etc. I can't run your code due to the constants defined in it, but it clearly communicates the right idea. It's good stuff for people to have, even in closed issues are I urge everyone to search through open and closed issues for answers.

It's always great to hear from a happy user. I dunno why PySimpleGUI programs are actually FUN to create, but I think they are. Maybe it's because I've disliked GUI programming my entire life and so it's quite new for me. I think the most fun is the "logic puzzle" behind it. Because you can write things like a single line GUI program, it opens up all kinds of fun stuff in a couple lines of code. It's fun to get so much done with so little effort. It's been called "cheating". I'll take that as a compliment 馃槒

@PySimpleGUI Sorry to bother you again, but how can I implement a multiline or similar in an "easy" way? I wanted to justify text inside a multiline, but it doesn't have justification parameter. I tried with Text but due to the extensiveness of the text (GPLv3 license file), moved to multiline due to the fixed size and scrollbar.
Thanks.
P.S: Didn't want to open a new issue, but I can open it if you prefer.

Open a new issue.

It's an enhancement request.

The way this would be implemented would be the same as the Text and Input elements. You set the justification when you create the element and that's what will be used throughout the history of the element.

The other option is for you to manage your own justification. This will be difficult if using proportional spaced fonts as you don't know how many characters to add.

Hopefully you're not in need of something that justifies on a per update basis. None of the other elements work this way. They're all set once and forget.

Thank you very much, I opened a new issue and answered in #3199 .

Was this page helpful?
0 / 5 - 0 ratings