使PySimpleGUI单选按钮生成事件 [英] Make PySimpleGUI Radio Buttons generate events

查看:1817
本文介绍了使PySimpleGUI单选按钮生成事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去,要使用Matplotlib Widgets构建单选按钮并将按钮名称打印到终端,我已经这样做了:

In the past, to build a radio button with Matplotlib Widgets and print the pushed button name to the terminal I have done this:

import matplotlib.pyplot as plt
from matplotlib.widgets import RadioButtons

def update(val):
    print(rb.value_selected)
    fig.canvas.draw_idle()

fig, ax = plt.subplots(1, 1)
ax = plt.axes([0.5, 0.4, 0.1, 0.15], facecolor='gray')
rb = RadioButtons(ax,  ('pi', '42'), active=0)
rb.on_clicked(update)
plt.show()

单选按钮上的更改始终会生成一个事件.

此答案中的示例,看来我还需要包括一个额外的Read按钮;在用户按下Read之前,单选按钮的可见外观可能与我的脚本认为用户想要的外观有所不同.操纵单选按钮似乎不会生成事件.然后,您必须按下第二个按钮,显示嘿!我已经下定决心,现在看看!

Looking at the example in this answer it seems I need to also include an extra Read button; the visible appearance of the radio buttons can be different than what my script thinks the user wants until the user presses Read. Manipulating the radio buttons does not seem to generate an event. You have to then push a second button that says Hey! I've made up my mind, now take a look!

import PySimpleGUI as sg

layout = [[sg.Radio('pi', 'num', default=True) ,
           sg.Radio('42', 'num')],
          [sg.Button('Read')]]

window = sg.Window('Radio Button Example', layout)

while True:             # Event Loop
    event, values = window.Read()
    if event in (None, 'Cancel'):
        break
    print(event, values)

window.close()

此评论

我认为您遇到了一个错误,或者未实现单选按钮enable_events.我以为是,但可能不在Qt上.我将其作为优先事项并查看代码.

I think you've hit either a bug or the Radio Buttons enable_events isn't implemented. I thought it was but may not be on Qt. I'll make it a priority and look at the code.

这使我认为,当更改单选按钮而不需要单独的按钮时,应该有一种在PySimpleGUI中生成事件的方法,但是我不知道是否有一个.

which makes me think there ought to be a way to generate an event in PySimpleGUI when a radio button is changed without need for a separate button, but I can't figure out if there is one.

问题:PySimpleGUI单选按钮是否可以在更改时生成事件?

Question: Is there a way for PySimpleGUI Radio Buttons to generate events when changed?

推荐答案

enable_events参数指定了某些更改时,将生成事件.记录在这里: https://pysimplegui.readthedocs.io/en/latest/#radio-element

Generating events when something changes is specified by the enable_events parameter. Documented here : https://pysimplegui.readthedocs.io/en/latest/#radio-element

尝试的例子.也适用于PySimpleGUIQt.

Example to try. Works for PySimpleGUIQt too.

import PySimpleGUI as sg

layout = [  [sg.Text('Radio Button Events')],
            [sg.Radio('1', 1, enable_events=True, key='R1'), sg.Radio('2',1, enable_events=True, key='R2')],
            [sg.Button('Go'), sg.Button('Exit')]  ]

window = sg.Window('Window Title', layout)

while True:             # Event Loop
    event, values = window.read()
    print(event, values)
    if event in (None, 'Exit'):
        break
window.close()

这篇关于使PySimpleGUI单选按钮生成事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆