1

I am using tkinter as a front end for Cisco IOS automation, but the problem i face is i need to have check-boxes available, if checked the text associated to it should be passed to the Cisco IOS. I tried to look into tkinter documentation but no luck.

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)
James Z
  • 12,209
  • 10
  • 24
  • 44
  • I tried but Var catches only whether the checkbox is checked or not. i want the text that is associated with the checkbox. In my case 'show cdp neighbor' should be retrieved once selected. – firdous ahmed reshi Aug 14 '17 at 11:34
  • Ah, my bad. So you are looking for `cv1["text"]` this value? – Lafexlos Aug 14 '17 at 11:55

2 Answers2

1

This is actually pretty simple:

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

You can use .cget() to retrieve the value of a Tkinter attribute. In the case above, you are printing the attribute text from the variable checkbutton which contains a predefined Tkinter Checkbutton element.


You can also do this directly from the Checkbutton by assigning a command to it. Meaning that the value would be received every time the Checkbuttons state updated

from tkinter import *

root = Tk()

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

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

checkbutton.pack()

root.mainloop()
Ethan Field
  • 4,646
  • 3
  • 22
  • 43
  • I have one small doubt now i am able retrieve text with cget. i wrote a function which checks whether checkbox is called or not based on that append text. But I see text is not getting appended to the list. import tkinter command =[] def get_command(): if c1==1: global command return lambda: command.append(c1.cget('text')) c1 = Tkinter.Checkbutton(SecondFrame,text='show cdp neighbor',command=get_command) c1.grid(row=3, column=5, sticky='S', padx=5, pady=2) – firdous ahmed reshi Aug 15 '17 at 21:07
  • Code doesn't format properly when put into a comment, can you instead describe what it is you are trying to achieve and I will see if I can help? – Ethan Field Aug 16 '17 at 08:48
0

if checked the text associated to it should be passed to the Cisco IOS

Indicate that you need to assign a variable with Widget.text (here Checkbutton.text).

1. Get Widget.text value


From TkDocs:

text: The label displayed next to the checkbutton. Use newlines ('\n') to display multiple lines of text.

To get this property:

  • by Checkbutton.cget() (see answer above)
  • directly access by Checkbutton.text or Checkbutton["text"]

Here, cv1.text or cv1["text"]

2. Assign variable when checked


As using Widget.bind() to bind click event to Checkbutton instance is not working on my machine, here I

assign a callback function to Checkbutton.command and check by Checkbutton.variable if the button has been checked.

from Tkinter import *  # import tkinter if using python3
Var = IntVar(0)  # use IntVar to store stat whether the button is checked
# If use StringVar() for variable, set `Checkbutton.onstate` and `Checkbutton.offstate`
# see TkDocs above for details
def callback():  # command callback doesn't needed Event as argument
  if Var.get() == 0:  # not checked
    return  # do nothing
  global command
  command.append(cv1.text)
cv1 = Checkbutton(SecondFrame, text="show cdp neighbor", command=callback)
cv1.grid(row=3, column=5, sticky="S", padx=5, pady=2)

Here variable is not Checkbutton.variable:

variable: The control variable that tracks the current state of the checkbutton; see Section 52, “Control variables: the values behind the widgets”. Normally this variable is an IntVar, and 0 means cleared and 1 means set, but see the offvalue and onvalue options above.


related questions:

Aoba K
  • 11
  • 3