前提・実現したいこと
モジュール内部から、メインの関数を実行させたいです。
ウィンドウと黒いキャンバスを作成し、モジュールの関数を呼び出すボタン(module)を設けます。
次に、モジュールの関数が実行され、ボタン(button)が生成されます。
そのボタンを押したときに、メインの中にあるキャンバスを削除する関数(func)を呼び出させたいのですが、やり方がわかりません。
発生している問題・エラーメッセージ
C:\python>main1.py Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\owner\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1699, in __call__ return self.func(*args) File "C:\python\module1.py", line 7, in push btn2["command"] = func NameError: name 'func' is not defined
該当のソースコード
Python
1**main.py** 2 3import tkinter 4import module1 5 6#キャンバス削除関数 7def func(): 8 canvas.delete("all") 9 10#ウィンドウ作成 11root = tkinter.Tk() 12root.minsize(900,460) 13#キャンパス作成 14canvas = tkinter.Canvas(width=900, height=460) 15canvas.place(x=0, y=0) 16canvas.create_rectangle(0,0,620,434,fill="black") 17 18#モジュール読み込み 19button2 = module1.Button2() 20 21#ボタン作成 22btn = tkinter.Button(text="module") 23btn.place(x=0, y=0) 24btn["command"] = button2.push 25 26root.mainloop() 27
**module1.py** import tkinter class Button2: def push(self): btn2 = tkinter.Button(text="button") btn2.place(x=100, y=0) btn2["command"] = func
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/01/31 11:31 編集
2018/01/31 11:38
2018/01/31 11:58