28
loading...
This website collects cookies to deliver better user experience
get()
and set()
methods for operating on them. To read the value of the variable, use get()
. To put the value into the variable, use set()
x = StringVar() # Holds a string; default value ""
x = IntVar() # Holds an integer; default value 0
x = DoubleVar() # Holds a floating point integer ; default value 0.0
x = BooleanVar() # Holds a Boolean, returns 0 for False and 1 for True
>>> a=IntVar() # You cannot declare a IntVar() without importing Tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'IntVar' is not defined
>>> from tkinter import *
>>> a=Intvar() # mind the Spelling!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Intvar' is not defined
>>> a=IntVar() # You cannot declare a IntVar() before creating a Tk() instance.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\aatma\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 539, in __init__
Variable.__init__(self, master, value, name)
File "C:\Users\aatma\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 346, in __init__
master = _get_default_root('create variable')
File "C:\Users\aatma\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 297, in _get_default_root
raise RuntimeError(f"Too early to {what}: no default root window")
RuntimeError: Too early to create variable: no default root window
>>> master=Tk() # A blank Tkinter window opens
>>> a=IntVar() #IntVar() is a class and we are creating an instance of the class.
>>> a.set(1) #set the value of a to 1
>>> a.get() # get (return) the value of a.
1
>>> a+2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'IntVar' and 'int'
>>> a.get()+2
3
>>> b=BooleanVar()
>>> b.set('True') # set b to 'True'
>>> b.get()
True
>>> c=DoubleVar()
>>> c.get()
0.0
>>> c.set(3)
>>> c.get() #automatic float conversion
3.0
>>> a.get()+c.get() # type converts to floating point automatically when float and integer added.
4.0
>>> d=StringVar()
>>> d.set("hello")
>>> str(c.get())+d.get() # the returned values can be used in operations.
'3.0hello'
>>> a.set(b.get()) # true=1
>>> a.get()
1
>>> a.set(c.get()) # type conversion automatic
>>> a.get()
3
>>> a.set(2.9) # floating points truncated.
>>> a.get()
2
>>> a=d
>>> a.get()
'hello'
>>> a=IntVar()
>>> a=1
>>> a.set(1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'set'
Intvar()
. Majority of the steps are the same as yesterday, with radio-buttons replacing checkboxes. So please study the program carefully and master Tkinter Radio-buttons. Comments are put wherever required.from tkinter import *
master= Tk()
master.title("RadioButton Demo...")
master.geometry("300x200")
v = IntVar()
v.set(-1) #initializing the choice, i.e. no default choice
languages = ["Python","Java","Swift","JavaScript"]
# making an array to choose the programming languages from
Stringlbl="My Favourite programming language is.... "
# text to display in the label
def showchoice(): # function to display the result
ResultLabel.config(text= Stringlbl +languages[v.get()])
ResultLabel=Label(master,text=Stringlbl)
# Make the label
ResultLabel.pack()
#Set the radiobuttons
Radiobutton1=Radiobutton(master,text=languages[0],variable=v,command=showchoice, value=0)
Radiobutton2=Radiobutton(master,text=languages[1],variable=v,command=showchoice, value=1)
Radiobutton3=Radiobutton(master,text=languages[2],variable=v,command=showchoice, value=2)
Radiobutton4=Radiobutton(master,text=languages[3],variable=v,command=showchoice, value=3)
Radiobutton1.pack()
Radiobutton2.pack()
Radiobutton3.pack()
Radiobutton4.pack()
master.mainloop()