2016-12-10 20 views
0

私は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() 

Errorscr

答えて

0

私は回避策を得た。私はツールバーモジュール(すなわち、tBarCall.py)内のDICTionaryを使用して、ボタンの詳細を保存し、追加し、削除するなどしました。そして、この辞書を呼び出しモジュール、すなわちtBarCall.pyに返しました。今、私はこの辞書を使ってボタンを無効にすることができます。

tBar.py

retButtonIdentifier = {"bSave" : bSave} 
retButtonIdentifier["bFirst"]=bFirst  

return retButtonIdentifier 

tBarCall.py

self.dictButtonIdentifier = tBar.attach_toolbar(self.rootClient, "I", save_record, first_record) 

self.dictButtonIdentifier["bSave"].configure(state="disabled")    
self.dictButtonIdentifier["bFirst"].configure(state="disabled")      
:私は追加のコーディングは次のとおり
関連する問題