[英]Return documentation on class constructor method WITHOUT using help or .__doc__
我試圖讓Tkinter GUI包適合python,我想創建一個gui函數,該函數將使用名為info
/單個關鍵字突出顯示的listbox
和按鈕命令返回所有方法和屬性docstring,如下所示:
我試圖在檢索curselection時使用get
方法請求從單擊info
的cli上的列表框的關鍵字獲取信息,但是框中的大多數關鍵字都是字符串對象,因此我不能使用.__doc__
方法Tk()實例對象,如何從Tk()實例的目錄列表中檢索每個方法的文檔字符串?
原始代碼:
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()
再次,信息按鈕將在列表框中生成關鍵字的文檔,但是我不確定如何將關鍵字作為對象檢索以正確獲取文檔字符串,我們將提供任何幫助
使用內置的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創建更好的幫助菜單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.