2017-06-10 40 views
-1

別のクラスから関数を呼び出すために保存ボタンを取得しようとしています。保存ボタンをクリックしたいと思うので、毎回「こんにちは人」が印刷されるはずです。ただし、保存ボタンを操作するのに問題があります。あなたのdocumentMakerクラスでPython 3 Tkinter - 別のクラスから関数を呼び出す方法

import tkinter as tk 
from tkinter import filedialog 


class Application(tk.Frame): 
    def __init__(self, parent): 
     tk.Frame.__init__(self, parent) 
     self.parent = parent 
     self.pack() 
     self.createWidgets() 

    def createWidgets(self): 

     #save button 
     self.saveLabel = tk.Label(self.parent, text="Save File", padx=10, pady=10) 
     self.saveLabel.pack() 
     #When I click the button save, I would like it to call the test function in the documentMaker class 
     self.saveButton = tk.Button(self.parent, text = "Save", command = documentMaker.test(self)) 
     self.saveButton.pack() 


class documentMaker(): 
    def test(self): 

     print ("hello people") 

root = tk.Tk() 
app = Application(root) 
app.master.title('Sample application') 
object = documentMaker() 
object.test() 

app.mainloop() 
+1

:あなたはそれがstaticmethodになりたいしなかった場合、あなたはにインスタンスメソッド、それを維持し、コマンドラインの変更を持っている可能性があり44012740 /選択されたファイル名をファイルに保存する方法から別の機能へ –

+0

[google](https://www.google.com/search?q=how+to+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + aqs = chrome.1.69i57j0l5.10235j0j4&sourceid = chrome&ie = UTF-8#newwindow = 1&q = python + how + to + call + a + function + from + another + class + oq = how + to + call + a +関数+ + a + function + from + another + class)あなたのタイトル。この質問には多くの結果があります。この種の質問は、Googleとstackoverflowの回答を見つけるのは非常に簡単です。 –

+0

[別のクラスからクラスメソッドを呼び出す]の可能な複製(https://stackoverflow.com/questions/3856413/call-class-method-from-another-class) –

答えて

0

@staticmethodtest方法を変更します。

class documentMaker(): 
    @staticmethod 
    def test(cls): 
     print ("hello people") 

を次に、あなたのsaveButtonのコマンドが使用できます

command = documentMaker.test 

staticmethodがクラスにバインドされています、インスタンスメソッドのようなクラスのインスタンスにではありません。クラスの名前から直接呼び出すことができます。私はhttps://stackoverflow.com/questions/しばらく戻って答え、この質問を参照してください

command = documentMaker().test 
関連する問題