【问题标题】:How to read the result of a Radiobutton with Python如何使用 Python 读取单选按钮的结果
【发布时间】:2017-03-15 16:30:00
【问题描述】:

我想在 Python2.7 中添加一些小部件,例如 Radiobutton,但我的代码无法编译。我想要的主要是当我点击IN或OUT时显示radio.on_clicked(sel)的结果。这是我的代码:

import pylab as plt
import numpy as np
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
from matplotlib.widgets import Button
from matplotlib.widgets import RadioButtons
import os
import Tkinter as tk
from tkFileDialog import askopenfilename
from tkFileDialog import asksaveasfile
import ntpath
from Tkinter import Label
from Tkinter import Listbox
from Tkinter import END
import Tix
import scipy

class Index(object):
    ind = 0

    def pre(self, event):
        print radio.on_clicked(sel) 

callback = Index()

axplot = plt.axes([0.11,0.92,0.08,0.07])
bplot = Button(axplot, 'Plot')
bplot.on_clicked(callback.pre)

choice = plt.axes([0.44,0.92,0.08,0.07])
radio = RadioButtons(choice, ('IN', 'OUT'))
def sel(var):
    if var == "IN":
        print "IN"
        return True
    elif var == "OUT":
        print "OUT"
        return False

plt.show()

但是当我单击单选按钮时,我在控制台中什么也看不到。而且我想保留类索引来做到这一点。

非常感谢您的帮助!

【问题讨论】:

    标签: python python-2.7 python-3.x numpy matplotlib


    【解决方案1】:

    print radio.on_clicked(sel) 行根本没有任何意义。
    radio.on_clicked(sel) 在 matplotlib 回调注册表中注册一个回调,并返回该回调在注册中的编号。因此,第一次调用它时,它可能返回 1,第二次返回 2 等等。您调用的次数越多,您最终在回调注册表中拥有的相同回调就越多,而其中一个就足够了。

    可能是以下内容正在做您需要的事情。

    import pylab as plt
    from matplotlib.widgets import Button
    from matplotlib.widgets import RadioButtons
    
    def sel(var):
        if var == "IN":
            print "IN"
            return True
        elif var == "OUT":
            print "OUT"
            return False
    
    def pre(event):
        print "You clicked the button"
    
    axplot = plt.axes([0.11,0.92,0.08,0.07])
    bplot = Button(axplot, 'Plot')
    bplot.on_clicked(pre)
    
    choice = plt.axes([0.44,0.92,0.08,0.07])
    radio = RadioButtons(choice, ('IN', 'OUT'))
    radio.on_clicked(sel)
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      • 2013-10-02
      • 2017-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多