2017-09-20 23 views
-1

別のプログラムでtkinterウィンドウのボタンをクリックすると、pythonプログラム(ex:program.py)を実行したいと思います。しかし、モジュールからクラスをインポートすると、クラスが実行されます。クリックすると、モジュールを実行するボタンを取得するにはどうすればよいですか?どんな支援も大歓迎です。Tkinterボタン - インポートと実行モジュール

ラン(program.py)へ

その他のモジュール:Tkinterのボタンで

class sampleProgram(): 
    def DoSomething(): 
     print('Do Something') 

モジュール:

from program import DoSomething 

class Window(Frame) 
    def __init__(self,master = None): 
     <Stuff In Window> 

    def addWidgets(self): 
     <Widgets To Add> 

    def init_window(self): 
     self.pack(fill=BOTH, expand=1) 
     RunButton = Button(self, text="Run", command=<**What Goes Here To Run sampleProgram?**>) 
+0

'class'ではなく' class'(すべて小文字)を意味すると仮定して、 'program'モジュールのインポートに問題はありません。 – BlackJack

+0

@BlackJack - お返事ありがとうございます。私は質問を編集しましたが、@ ReblochonMasqueの元の応答に基づいて動作させることができました。関数をクラスに入れるのではなく、 'staticmethod'を使うことについてのあなたのコメントを説明できますか?私はそれを書く良い方法を学ぶ機会に感謝します! –

+0

メソッドが最初の引数として呼び出されたインスタンスを受け取らない場合、メソッドは実際にはメソッドではなく、これは[staticmethod()](https://docs.python.org/2/)で明確にする必要があります。 library/functions.html#staticメソッド)。それ以外の場合はインスタンス上で呼び出すことはできません。また、関数/ "静的メソッド"をクラスに入れるのはコードの匂いです。まれに、それは理にかなっていますが、あなたがなぜクラスに詰め込まれた関数の代わりに単に普通の関数ではないのかを説明することができる場合のみです。 – BlackJack

答えて

0

sampleProgramクラスのDoSomethingに電話する必要があります。このためには、インポートする必要があります。 Tkinterのボタンで

Class sampleProgram(): 
    def DoSomething():    # <--- this is a staticmethod 
     print('Do Something') 

モジュール:

from program import sampleProgram # <--- import the class sampleProgram 

Class Window(Frame) 
    def __init__(self,master = None): 
     <Stuff In Window> 

    def addWidgets(self): 
     <Widgets To Add> 

    def init_window(self): 
     self.pack(fill=BOTH, expand=1) 
     RunButton = Button(self, text="Run", command=sampleProgram.DoDomething) 

あなたはrunButton commandからsampleProgram.DoDomething staticmethodをバインドします。ボタンをクリックすると、このコマンドが呼び出されます。

+0

'DoSomething'はクラスメソッドではありません。 'classmethod'でそれを飾る必要があります。クラスオブジェクトを最初の引数として取る必要があります。実際には 'staticmethod'が必要なのかもしれません。次に、なぜ機能が「クラス」に移行するのかという疑問が湧きます。 – BlackJack

+0

良い点、私は投稿を修正した –

1

from program import DoSomething

そのボタンのコマンドでは、あなたは、単にdef DoSomething()

を呼び出すことができます

RunButton = Button(self, text="Run", command=DoSomething)

関連する問題