简体   繁体   English

如何使用 python 中的选项制作弹出窗口 window?

[英]How do I make a pop-up window with choices in python?

For a project I am working on, I want to make a pop-up window with several different choices, that can return a value based on which choice the user picks;对于我正在处理的项目,我想制作一个弹出窗口 window 有几个不同的选项,可以根据用户选择的选项返回一个值; I found solutions to get simple pop-ups, but not ones that return a value.我找到了获得简单弹出窗口的解决方案,但没有找到返回值的解决方案。 I am using Python 3.8.我正在使用 Python 3.8。

As barny suggested, PySimpleGUI is about as easy as it gets.正如 barny 所建议的,PySimpleGUI 非常简单。

What you've described is what's called a one-shot window in the PySimpleGUI Cookbook.您所描述的是 PySimpleGUI Cookbook 中所谓的一次性 window

These types of GUIs can be written as a single line of PySimpleGUI code because you don't need a full event loop.这些类型的 GUI 可以编写为一行 PySimpleGUI 代码,因为您不需要完整的事件循环。

import PySimpleGUI as sg

event, values = sg.Window('Choose an option', [[sg.Text('Select one->'), sg.Listbox(['Option a', 'Option b', 'Option c'], size=(20, 3), key='LB')],
    [sg.Button('Ok'), sg.Button('Cancel')]]).read(close=True)

if event == 'Ok':
    sg.popup(f'You chose {values["LB"][0]}')
else:
    sg.popup_cancel('User aborted')

After the call to Window, with the chained read call, you're provided the event used to close the window (which button or if closed with the "X") and the dictionary of values.在通过链式read调用调用 Window 之后,您将获得用于关闭 window(哪个按钮或如果使用“X”关闭)和值字典的事件。 In this case, your values dictionary will have a single item values['LB'] .在这种情况下,您的 values 字典将有一个项目values['LB'] For Listboxes, this value will be a list.对于列表框,此值将是一个列表。 To get the item chosen, you could reference values['LB'][0]要选择项目,您可以参考values['LB'][0]

PySimpleGui is the way to go for simplicity - and it seems to work with Python 3.8.3 on Windows 10.为简单起见,PySimpleGui 是通往 go 的方法 - 它似乎适用于 Windows 10 上的 Python 3.8.3。

Creating a simple gui dialog to make a selection can't get much easier than this (although it can also do much more complex UI when needed):创建一个简单的 gui 对话框来进行选择没有比这更容易的了(尽管它也可以在需要时做更复杂的 UI):

import PySimpleGUI as sg

#sg.theme('DarkAmber')   # Add a touch of color

options = ['Option a','Option b','Option c']

# All the stuff inside your window.
layout = [ 
            [sg.Text('Select one->'), sg.Listbox(options,select_mode=sg.LISTBOX_SELECT_MODE_SINGLE,size=(20,len(options)))],
            [sg.Button('Ok'), sg.Button('Cancel')]
        ]

# Create the Window
window = sg.Window('Make your choice', layout)

# Event Loop to process "events" and get the "values" of the input
while True:
    event, values = window.read()
    print( f"event={event}" )
    if event is None or event == 'Ok' or event == 'Cancel': # if user closes window or clicks cancel
        break
        
# close  the window        
window.close()

if event == "Cancel":
    print( "You cancelled" )
else:
    print('You entered ', values[0])
    sg.popup( f"You selected {values[0]}" )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM