2017-08-14 118 views
0

我使用tkinter作爲Cisco IOS自動化的前端,但我面臨的問題是我需要有複選框可用,如果選中相關的文本應傳遞給思科IOS。 我試圖查看tkinter文檔,但沒有運氣。tkinter複選框設置值,如果選中

Var = Tkinter.StringVar() 
cv1 = Tkinter.Checkbutton(SecondFrame,text='show cdp neighbor', variable=Var) 
cv1.grid(row=3, column=5, sticky='S', padx=5, pady=2) 
+0

我試過了,但是Var只捕獲了複選框是否被選中。我想要與複選框關聯的文本。在我的情況下,'show cdp neighbour'應該被選中後被檢索。 –

+0

啊,我的不好。所以你正在尋找'cv1 [「text」]這個值? – Lafexlos

回答

1

這其實很簡單:

from tkinter import * 

root = Tk() 

def command(): 
    print(checkbutton.cget("text")) 

checkbutton = Checkbutton(root, text="Retrieve This Text") 
button = Button(root, text="Ok", command=command) 

checkbutton.pack() 
button.pack() 

root.mainloop() 

您可以使用.cget()檢索Tkinter屬性的值。在上面的情況下,您正在打印來自變量checkbutton的屬性text,該變量包含預定義的元素TkinterCheckbutton


您也可以直接從Checkbutton通過給它分配一個命令來做到這一點。意思是每當Checkbutton的狀態更新時會收到這個值

from tkinter import * 

root = Tk() 

def command(): 
    print(checkbutton.cget("text")) 

checkbutton = Checkbutton(root, text="Retrieve This Text", command=command) 

checkbutton.pack() 

root.mainloop() 
+0

非常感謝Ethan .. :) –

+0

如果這回答你的問題,請你可以標記爲接受其他用戶看到? –

+0

我現在有一個小疑問,我可以用cget檢索文本。我寫了一個函數,該函數根據該附加文本檢查複選框是否被調用。但我看到文字沒有被追加到列表中。 進口的Tkinter 命令= [] DEF GET_COMMAND(): 如果c1 == 1: 全局命令 返回拉姆達:command.append(c1.cget( '文本')) C1 = Tkinter.Checkbutton(SecondFrame, text ='show cdp neighbor',command = get_command) c1.grid(row = 3,column = 5,sticky ='S',padx = 5,pady = 2) –