2016-04-11 5 views
0

私はGUIには新しく、変数の継承に関する簡単な質問があります。Python QtGui:クラス間での変数の継承

私のGUIには2つのボタンがあり、1つはxlsxファイルを選択し、もう1つはグラフを表示します。 ...

(私はグラフのコードのビットに追加しました)

class Example(QtGui.QWidget): 

def __init__(self): 
    super(Example, self).__init__() 

    self.initUI() 

def initUI(self): 

    vBoxLayout = QtGui.QVBoxLayout(self) 

    file_btn = QtGui.QPushButton('Select File', self) 
    file_btn.clicked.connect(self.get_graph_file) 

    graphing_btn = QtGui.QPushButton('Plot Graph', self) 
    graphing_btn.clicked.connect(Plotting_Graph) 

    self.show() 

def get_graph_file(self): 

    fname_graphfile = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/Users/.../', 'excel files (*.xlsx)') 

...第二はfname_graphfileを継承し、それをグラフ化する必要があります最初のclassは、下のボタンを設定し、ファイルを選択し、

class Plotting_Graph(Example): 

def __init__(self): 

    self.PlottingGraph() 

def PlottingGraph(self): 

    xl = ef(fname_graphfile[0])...... 

実行すると、エラーglobal name 'fname_graphfile' is not definedが返されます。

classで定義したものを覚えておくには、どのように2番目のclassを取得しますか?

答えて

1

fname_graphfileget_graph_fileので、他の方法は、あなたが何をすべきか、それへのアクセスを持っていないローカル変数である、受け入れることのいずれかの方法からアクセスして、Plotting_Graph.PlottingGraphに引数を追加することができますので、それインスタンス属性作るですxlsxメソッドのパラメータとしてファイルとこの

from PyQt4 import QtCore, QtGui 
import sys 

class Example(QtGui.QWidget): 

    def __init__(self): 
     super(Example, self).__init__() 
     self.initUI() 

    def initUI(self): 
     self.fname_graph_file = '' 
     file_btn = QtGui.QPushButton('Select File', self) 
     file_btn.setGeometry(100, 100, 150, 30) 
     file_btn.clicked.connect(self.get_graph_file) 

     graphing_btn = QtGui.QPushButton('Plot Graph', self) 

     plot = Plotting_Graph() 
     graphing_btn.clicked.connect(lambda: plot.PlottingGraph(self.fname_graph_file)) 

     self.show() 

    def get_graph_file(self): 

     self.fname_graph_file = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home', 'excel files (*.xlsx)') 

class Plotting_Graph(Example): 

    def __init__(self): 
     pass 

    def PlottingGraph(self, fname_graphfile): 

     print(fname_graphfile) 
     # xl = ef(fname_graphfile[0]) 

app = QtGui.QApplication([]) 
a = Example() 
app.exec_() 
のようになります

あなたの最終的なコードをクリックして、ボタンから、それにself.fname_graphfileを渡します