2017-03-15 57 views
1

我想用Python2.7添加一些像Radiobutton這樣的小部件,但是我的代碼不能編譯。我想要的主要是當我點擊IN或OUT時顯示radio.on_clicked(sel)的結果。這是我的代碼:如何用Python讀取Radiobutton的結果

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

但是,當我點擊我單選框沒有看到在控制檯中。而且我還想讓班級索引來做到這一點。

非常感謝您的幫助!

回答

0

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