Daily Python

An aggregation of blogs and posts in Python

Wednesday, January 20, 2021

Mike Driscoll: PySimpleGUI: Working with Multiple Windows

When you are creating graphical user interfaces (GUIs), you will often find that you need to create more than one window. In this tutorial, you will learn how to create two windows with PySimpleGUI.

PySimpleGUI is one of the easiest Python GUIs to get started with. It wraps other Python GUIs and gives them a common interface. You can read more about it in my Intro to PySimpleGUI or in my article for Real Python, PySimpleGUI: The Simple Way to Create a GUI With Python.

Getting Started

You will want to install PySimpleGUI to get started using it. You can use pip for that:

python -m pip install pysimplegui

Creating a Modal Window

PySimpleGUI provides a Window Element that you use to display other Elements in, such as buttons, text, images, and more. These Windows can be made Modal. A Modal Window won’t let you interact with any other Windows in your program until you exit it. This is useful when you want to force the user to read something or ask the user a question. For example, a modal dialog might be used to ask the user if they really want to Exit your program or to display an end-user agreement (EULA) dialog.

You can create two Windows in PySimpleGUI like this:

import PySimpleGUI as sg

def open_window():
    layout = [[sg.Text("New Window", key="new")]]
    window = sg.Window("Second Window", layout, modal=True)
    choice = None
    while True:
        event, values = window.read()
        if event == "Exit" or event == sg.WIN_CLOSED:
            break
        
    window.close()


def main():
    layout = [[sg.Button("Open Window", key="open")]]
    window = sg.Window("Main Window", layout)
    while True:
        event, values = window.read()
        if event == "Exit" or event == sg.WIN_CLOSED:
            break
        if event == "open":
            open_window()
        
    window.close()


if __name__ == "__main__":
    main()

When you run this code, you will see a small Main Window that looks like this:

PySimpleGUI Main Window

If you click on the “Open Window” button, you will get a new Window that looks like this:

New Window in PySimpleGUI

This second window has a parameter named modal in it that is set to True. That means you cannot interact with the first Window until you close the second one.

Now let’s look at a way that you can shorten your code if you are creating a simple Window like the one above.

Creating a New Window In-Line

You don’t have to write a completely separate function for your secondary Window. If you’re not going to have a lot of widgets in the second Window, then you can create the Window as a one or two-liner.

Here is one way to do that:

import PySimpleGUI as sg


def main():
    layout = [[sg.Button("Open Window", key="open")]]
    window = sg.Window("Main Window", layout)
    while True:
        event, values = window.read()
        if event == "Exit" or event == sg.WIN_CLOSED:
            break
        if event == "open":
            if sg.Window("Other Window", [[sg.Text("Try Again?")], 
                                          [sg.Yes(), sg.No()]]).read(close=True)[0] == "Yes":
                print("User chose yes!")
            else:
                print("User chose no!")
        
    window.close()


if __name__ == "__main__":
    main()

In this example, when you click the “Open Window” button, it creates the secondary Window in a conditional statement. This Window calls read() directly and closes when the user chooses “Yes”, “No” or exits the Window. Depending on what the user chooses, the conditional will print out something different.

Wrapping Up

PySimpleGUI lets you create simple as well as complex user interfaces. While it’s not covered here, you can also use sg.popup() to show a simpler dialog to the user. These dialogs are not modal and not fully customizable like a regular Window is.

Give PySimpleGUI a try and see what you think.

Related Reading

  • A Brief Intro to PySimpleGUI
  • The Demos for PySimpleGUI
  • Real Python – PySimpleGUI: The Simple Way to Create a GUI With Python

The post PySimpleGUI: Working with Multiple Windows appeared first on Mouse Vs Python.



from Planet Python
via read more
at January 20, 2021
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Labels: Planet Python, Python

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...

  • Learn PyQt: Creating multiple windows in PyQt5/PySide2
    In an earlier tutorial we've already covered how to open dialog windows. These are special windows which (by default) grab the focus o...
  • Python GUIs: PySide2 vs PySide6: What are the differences, and is it time to upgrade? — Is it time to upgrade?
    If you are already developing Python GUI apps with PySide2, you might be asking yourself whether it's time to upgrade to PySide6 and use...
  • Python GUIs: Packaging PyQt5 applications for Windows, with PyInstaller & InstallForge — Turn your PyQt5 application into a distributable installer for Windows (updated for 2022)
    There is not much fun in creating your own desktop applications if you can't share them with other people — whether than means publishin...

Blog Archive

  • February 2022 (41)
  • January 2022 (166)
  • December 2021 (131)
  • November 2021 (191)
  • October 2021 (222)
  • September 2021 (208)
  • August 2021 (198)
  • July 2021 (257)
  • June 2021 (275)
  • May 2021 (355)
  • April 2021 (309)
  • March 2021 (289)
  • February 2021 (266)
  • January 2021 (288)
  • December 2020 (248)
  • November 2020 (271)
  • October 2020 (253)
  • September 2020 (320)
  • August 2020 (378)
  • July 2020 (386)
  • June 2020 (359)
  • May 2020 (289)
  • April 2020 (340)
  • March 2020 (300)
  • February 2020 (240)
  • January 2020 (275)
  • December 2019 (255)
  • November 2019 (279)
  • October 2019 (310)
  • September 2019 (262)
  • August 2019 (321)
  • July 2019 (451)
  • June 2019 (288)
  • May 2019 (335)
  • April 2019 (286)
  • March 2019 (294)
  • February 2019 (271)
  • January 2019 (294)
  • December 2018 (310)
  • November 2018 (80)
  • October 2018 (2)
  • March 2017 (1)
  • February 2017 (1)
  • January 2017 (3)
  • December 2016 (3)
  • October 2016 (1)
  • September 2016 (1)
  • August 2016 (5)
  • July 2016 (1)

Labels

  • ALL YOUR BASE ARE BELONG TO US
  • Cool Python Codes
  • Doug Hellmann
  • ImportPython Blog
  • Inside the Head of Pydanny
  • Planet Python
  • Planet SciPy
  • Practical Business Python
  • PyBloggers
  • PyCharm – JetBrains Blog
  • PyCharm Blog
  • PyCharm: the Python IDE for Professional Developers – PyCharm Blog | JetBrains
  • PyMOTW – Doug Hellmann
  • Python
  • python – Red Hat Developer
  • python – Red Hat Developer Blog
  • python – RHD Blog
  • Python Coder
  • Python Insider
  • Python Software Foundation News
  • Python Tips
  • PythonAnywhere News
  • Real Python
  • Subclassed
  • The PyCon Blog
  • The Python Guru

Report Abuse

  • Home

Search This Blog

Simple theme. Powered by Blogger.