如何使用 Python 读取单选按钮的结果 [英] How to read the result of a Radiobutton with Python

查看:171
本文介绍了如何使用 Python 读取单选按钮的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to add some widgets like Radiobutton with Python2.7 but my code does not compile. What I want is mainly when I click on IN or OUT is to display the result of radio.on_clicked(sel). This is my code :

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()

但是当我单击 Radiobutton 时,我在控制台中什么也看不到.而且我也想保留班级索引来做到这一点.

But when I click on the Radiobutton I see nothing in the console. And also I want to keep the class Index to do this.

非常感谢您的帮助!

推荐答案

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

The line print radio.on_clicked(sel) does not make any sense at all.
radio.on_clicked(sel) registers a callback in the matplotlib callback registry and returns the number of this callback in the register. So the first time you call it, it may return 1, second time 2 etc. The more often you do it, the more of the same callbacks you end up having in the callback registry, whereas one of them is enough.

可能是以下内容满足您的需求.

It could be that the following is doing what you need.

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()

这篇关于如何使用 Python 读取单选按钮的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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