Professional Documents
Culture Documents
Day-28 Notes 100DOC
Day-28 Notes 100DOC
Day-28 Notes 100DOC
def print_scale_value(value):
print (value)
While Creating a Checkbutton , we use a option called variable which synchronize checkbutton with
tk.IntVar() to automatically update the value in tk.IntVar() when checkbutton is checked or unchecked.
def ch_btn():
print(checked_value.get())
checked_value = tkinter.IntVar() # returns 1 when button is checked and
returns 0 button is unchecked.
checkbtn = tkinter.Checkbutton(text="Checkbox", variable=checked_value,
command=ch_btn)
checkbtn.pack()
While defining Radiobuttons , all radiobuttons should share common instance of IntVar.
def radio_use():
print(tkinter.IntVar().get() )
radiobtn1 = tkinter.Radiobutton(text="Option 1" , variable=tkinter.IntVar() ,
value = 1, command=radio_use)
radiobtn2 = tkinter.Radiobutton(text="Option 2" , variable=tkinter.IntVar() ,
value = 2, command=radio_use)
radiobtn1.pack()
radiobtn2.pack()
It won’t syncrohnise rdbtn1 and rdbtn2 and radio_use instance because all are different.
def radio_use():
print(rdb.get())
rdb=tkinter.IntVar()
radiobtn1 = tkinter.Radiobutton(text="Option 1" , variable=rdb, value = 1,
command=radio_use)
radiobtn2 = tkinter.Radiobutton(text="Option 2" , variable=rdb , value = 2,
command=radio_use)
radiobtn1.pack()
radiobtn2.pack()
For laying out the widget on window , we use .pack() function , but we didn’t get independence to pack
the widget on a particular position on the graph window.
Therefore , a new place is used .place(x,y) which do same work as .pack() , but also take coordinates x
and y to place the widget on (x,y).
But grid and place are compatible which each other but mostly grid is preferred.
Canvas widget is a component in tkinter on which we can layout multiple widget on each other.
Canvas.create_text(*args , **kwgs) *args include xcor , ycor and **kwargs=text,fill ,font option . fill is for
color,font takes tuple.