前提・実現したいこと
Python,TKinterでフォームアプリを作成中です。タブ移動後に何らかの処理をするためにマウス左ボタンリリース時の<ButtonRelease-1>で動作するプログラム(onefile.py)を作りました。これは正常に動作します。
これを親子のparent.py,child1.py,child2.pyに分割したところ下記のエラーが発生します。対策をご教授ください。「self」、「event」は理解不足の状態です
ここに質問の内容を詳しく書いてください。
(例)PHP(CakePHP)で●●なシステムを作っています。
■■な機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\akiteru\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) TypeError: callback() missing 1 required positional argument: 'event'エラーメッセージ
該当のソースコード
正常に動作する一ファイルでの記述(onefile.py) # ******* onefile.py import tkinter as tk import tkinter.ttk as ttk class MainWindow(ttk.Frame): def __init__(self, parent): super(MainWindow, self).__init__(parent) self.parent = parent #********画面作成 self.create_notebook() self.pack() def create_notebook(self): self.nb = ttk.Notebook(self,width=800, height=400) # タブの作成 self.tab1 = tk.Frame(self.nb) self.tab2 = tk.Frame(self.nb) self.nb.add(self.tab1, text='入力', padding=3) self.nb.add(self.tab2, text='出力', padding=3) # ********タアブ移動時タブのId入手 self.nb.bind('<ButtonRelease-1> ',self.callback) self.nb.pack(expand=1, fill='both') def callback(self,event): # *******移動後のタブIDを表示 new_tab_id = self.nb.index(self.nb.select()) print(new_tab_id) def quit(self, event=None): # 終了時の処理 self.master.destroy() application = tk.Tk() application.geometry("500x500") application.title('PyPost') window = MainWindow(application) application.protocol('WM_DELETE_WINDOW', window.quit) application.mainloop() 親子に分割したファイル(parent.py、 child1.py、 cild2.py) # ******* parent.py import tkinter as tk import tkinter.ttk as ttk import child1 as ch1 import child2 as ch2 class MainWindow(ttk.Frame): def __init__(self, parent): super(MainWindow, self).__init__(parent) self.parent = parent #********画面作成 ch1.create_notebook(self) self.pack() def quit(self, event=None): # 終了時の処理 self.master.destroy() application = tk.Tk() application.geometry("500x500") application.title('PyPost') window = MainWindow(application) application.protocol('WM_DELETE_WINDOW', window.quit) application.mainloop() #**** child1.py import tkinter as tk import tkinter.ttk as ttk import child2 as ch2 # **********画面作成 def create_notebook(self): self.nb = ttk.Notebook(self,width=800, height=400) # タブの作成 self.tab1 = tk.Frame(self.nb) self.tab2 = tk.Frame(self.nb) self.nb.add(self.tab1, text='入力', padding=3) self.nb.add(self.tab2, text='出力', padding=3) # ********タアブ移動時タブのId入手 self.nb.bind('<ButtonRelease-1> ',ch2.callback) self.nb.pack(expand=1, fill='both') # **** child2.py import tkinter as tk import tkinter.ttk as ttk # ******各種アクション集 def callback(self,event): new_tab_id = self.nb.index(self.nb.select()) print(new_tab_id)
試したこと
ここに問題に対して試したことを記載してください。
child2.py内の【def callback(self,event):】のeventを削除し【「def callback(self):」】とすると下記エラーメッセージとなります
Traceback (most recent call last):
File "C:\Users\akiteru\AppData\Local\Programs\Python\Python38\lib\tkinter_init_.py", line 1883, in call
return self.func(*args)
File "c:\PyPost\child2.py", line 7, in callback
new_tab_id = self.nb.index(self.nb.select())
AttributeError: 'Event' object has no attribute 'nb'
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー