Quantcast
  • Login
    • Account
    • Sign Up
  • Home
    • About Us
    • Catalog
  • Search
  • Register RSS
  • Embed RSS
    • FAQ
    • Get Embed Code
    • Example: Default CSS
    • Example: Custom CSS
    • Example: Custom CSS per Embedding
  • Super RSS
    • Usage
    • View Latest
    • Create
  • Contact Us
    • Technical Support
    • Guest Posts/Articles
    • Report Violations
    • Google Warnings
    • Article Removal Requests
    • Channel Removal Requests
    • General Questions
    • DMCA Takedown Notice
  • RSSing>>
    • Collections:
    • RSSing
    • EDA
    • Intel
    • Mesothelioma
    • SAP
    • SEO
  • Latest
    • Articles
    • Channels
    • Super Channels
  • Popular
    • Articles
    • Pages
    • Channels
    • Super Channels
  • Top Rated
    • Articles
    • Pages
    • Channels
    • Super Channels
  • Trending
    • Articles
    • Pages
    • Channels
    • Super Channels
Switch Editions?
Cancel
Sharing:
Title:
URL:
Copy Share URL
English
RSSing>> Latest Popular Top Rated Trending
Channel: Planet Python
NSFW?
Claim
0


X Mark channel Not-Safe-For-Work? cancel confirm NSFW Votes: (0 votes)
X Are you the publisher? Claim or contact us about this channel.
X 0
Showing article 14991 of 23315 in channel 44877200
Channel Details:
  • Title: Planet Python
  • Channel Number: 44877200
  • Language: English
  • Registered On: June 19, 2015, 2:31 pm
  • Number of Articles: 23315
  • Latest Snapshot: June 8, 2025, 5:56 am
  • RSS URL: http://planetpython.org/rss20.xml
  • Publisher: http://planetpython.org/
  • Description: Planet Python - http://planetpython.org/
  • Catalog: //python1233.rssing.com/catalog.php?indx=44877200
Remove ADS
Viewing all articles
Browse latest Browse all 23315
↧

Mike Driscoll: PySimpleGUI: Working with Multiple Windows

January 20, 2021, 7:06 am
≫ Next: Matthew Wright: Selecting in Pandas using where and mask
≪ Previous: Real Python: C for Python Programmers
$
0
0

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.

↧
Search

Remove ADS
Viewing all articles
Browse latest Browse all 23315

Trending Articles


Scuffham Amps - S-GEAR 2.6.0 VST, AAX, STANDALONE x86 x64 (R2R NO iLok2, +NO...

November 19, 2017, 7:24 am

Practice Sheet of Right form of verbs for HSC Students

September 22, 2019, 11:40 pm

VHSE First (1st) Allotment 2025 - vhscap.kerala.gov.in

May 24, 2025, 11:58 pm

UNIVERSE LEAGUE – UNIVERSE LEAGUE – WAR (We Are Ready) – EP [iTunes Plus M4A]

January 26, 2025, 11:12 am

City Hunter Teledrama – Episode 18 – 07th May 2016

May 6, 2016, 2:05 pm

Comment on Proposed Criteria for Identifying Predatory Conferences by Luke...

July 4, 2016, 3:53 am

Bureau of Internal Revenue: Regional Offices (Directory)

January 9, 2014, 11:06 pm

Kendrick Lamar – Not Like Us (2024) [24Bit-88.2kHz] [PMEDIA] ⭐️

May 14, 2024, 7:14 pm

Inception 2010 Hindi Dual Audio 650MB BRRip 720p ESubs HEVC

December 27, 2016, 4:23 pm

East Hull MD admits sexual assaults after another victim comes forward

January 14, 2013, 12:20 am

Download: FK ft Shenky – Nakuyewa ”Prod by: Shenky”

February 16, 2017, 4:24 pm

R. v. Sargeant, 2023 ONSC 6406 (CanLII)

November 13, 2023, 9:00 pm

Rajasthan Board 10th Result 2016 Roll No wise & Name Wise

August 20, 2016, 5:13 pm

Who’s been sentenced at Northampton Magistrates’ Court

March 30, 2019, 10:00 pm

मतलबी दोस्त स्टेट्स | Matlabi Dost Status in Hindi – Selfish Friends Status

February 13, 2020, 3:12 am

Family cries out as traditional ruler allegedly abducts brother, extorts N2.5m

September 15, 2024, 1:15 am

Long-Running Conflict In Springfield (MA) Gangland Sphere Has Manzi Family &...

December 26, 2017, 3:59 pm

Wondershare Filmora X v10.1.20.16 x64

February 8, 2021, 5:34 am

Man arrested after fracas in flat

January 16, 2017, 10:10 pm

Man charged in ongoing Sexual Assault Investigation Derek Nyilas, 46, Faces...

May 27, 2019, 8:00 am

Search

  • RSSing>>
  • Latest
  • Popular
  • Top Rated
  • Trending
© 2025 //www.rssing.com
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>