2016-12-19 2 views
0

Python用のTkinter GUIパッケージを使いやすくするために、ヘルプを使用せずにクラスコンストラクタメソッドに関するドキュメントを返す。

tkinter_method display

私がの検索時にgetメソッド要求を使用してinfoにクリックあたりCLIのリストボックスのキーワードから情報を取得しようとしている:の下に表示されているようにlistboxとボタンコマンドは、単一のキーワードのハイライトあたりinfoと呼ばれますcurselectionですが、ボックス内のキーワードのほとんどは文字列オブジェクトなので、CANN OT Tk()インスタンスオブジェクトのメソッド.__doc__を使用します.Tk()インスタンスのディレクトリリストから各メソッドのdocstringを取得するにはどうすればよいですか?

オリジナルコード:

from Tkinter import * 
import threading, sys 


def document(): 
    """ Define the function's purpose """ 
    cursor = listbox.curselection() 
    item = window_docs[int(cursor[0])] 
    print item 
    return 

win = Tk() 

Label(win, text="A list of the following packages from Tkinter:\n").pack(side="top") 

scrollbar = Scrollbar(win) 
types = len(dir(win)) #list of the different widgets accessible with Tkinter 

button = Button(win, text="quit?", command=win.quit) 

button.config(bg="#A57706", fg="#042029", relief="ridge", bd=3) 

button.pack(side="top") 

listbox = Listbox(win, yscrollcommand=scrollbar.set) 
listbox.config(height = "400", width="30") 

listbox.document = document #Bind the function to listbox constructor 

window_docs = {} 

for wid in range(0, types-1): 
     constructor = dir(win)[wid] #constructor method 
     listbox.insert(wid, constructor) 
     window_docs[wid] = constructor.__doc__ 

listbox.pack(side='top', fill="y") 

trigger = Button(win, text="info", command=lambda listbox=listbox: listbox.document()) 

trigger.place(x=20, y=30, width=30, height=15) 
scrollbar.config(command=listbox.yview) 
scrollbar.pack(side="right", fill="y") 

while True: 
     win.mainloop() 

はもう一度、情報ボタンは、リストボックス内のキーワードのドキュメントを生成しますが、私は適切に取得する対象としてキーワードを取得する方法がわかりませんよdocstring、任意のヘルプが評価されます

+0

なぜこのタグはC#ですか? – stuartd

+0

スタックのボットで私にオートレコメンドされたので、それを含めました – akiespenc

+0

^私はあなたのコメントで混乱しています、何を示唆していますか? – akiespenc

答えて

0

getattr組み込み関数を使用すると、オブジェクトを空の変数に戻すことができます。だからここで定義された関数documentは、ボタンコマンドinfo用で、より多くのロジックを追加します。

def document(): 
     """ Define the function's purpose """ 
     root = Tk() 
     cursor = listbox.curselection() 
     item = getattr(root, cursor) 
     print item 
     root.destroy() 
     return #exit from function 

これは私が探していた機能ですので、私は何をエクスポートすることにより、Tkinterのためのよりよいヘルプメニューを作成することができますコマンドラインのメソッド/属性

関連する問題