私は2つのTkInterモジュールを持っています。 1つはツールバーモジュール(tBar.py)で、もう1つは汎用モジュール(tBarCall.py)です。インポートされたモジュールからウィジェットを参照しています
1)ツールバーモジュール(tBar.py):ツールバーフレームを作成し、その上にボタン(保存、追加、削除など)を配置します。
2)クライアントマスターモジュール(tBarCall.py):ツールバーモジュールをインポートして実行します。その後、後の段階で、ツールバーフレームに配置されているボタンの一部を有効/無効にする必要があります。ボタンを有効/無効にしようとしているときに表示されるエラーを確認してください。
クライアントマスタモジュール内からツールバーモジュールをインポートできますが、ツールバーフレームのボタンを有効/無効(参照)できません。
どうすればいいですか?ここ
コードである:
=== tBar.py ===
import tkinter as tk
def attach_toolbar(rootWindow, action, save_record, first_record):
if action == "I":
fToolbar = tk.Frame(rootWindow, padx = 1, pady = 1, bg="RED")
fToolbar.grid(row=0, column = 0, sticky='w') #pack(anchor="nw",expand=1) #
bSave = tk.Button(fToolbar, text="save", command=save_record) #width=6, height=2, text = "Save\nrecord", font=("Calibri", 8),
bFirst = tk.Button(fToolbar, text="first", command=first_record) #width=6, height=2, text = "First\nrecord", font=("Calibri", 8),
bSave.pack(side="left")
bFirst.pack(side="left")
else:
bFirst.configure(state="disabled")
=== tBarCall.py ===
import tkinter as tk
import tBar
def save_record():
print ("save_record")
def first_record():
print ("first_record")
class startModule:
def __init__(self, rootWindow):
print("__init__")
self.rootWindow = rootWindow
self.rootClient = tk.Toplevel(self.rootWindow)
self.rootClient.geometry('1300x650+1+1')
tBar.attach_toolbar(self.rootClient, "I", save_record, first_record)
#tBar.attach_toolbar(self.rootClient, "D", save_record, first_record) #UnboundLocalError: local variable 'bFirst' referenced before assignment
#bFirst.configure(state="disabled") #NameError: name 'bFirst' is not defined
#self.rootClient.bFirst.configure(state="disabled") #AttributeError: 'Toplevel' object has no attribute 'bFirst'
#self.rootClient.fToolbar.bFirst.configure(state="disabled") #AttributeError: 'Toplevel' object has no attribute 'fToolbar'
if __name__ == "__main__":
root = tk.Tk()
startModule(root)
root.mainloop()