Day-28 Notes 100DOC

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Scale widget in Tkinter has command keyword argument in which a callback function is passed , and it

takes one argument which is current value of the scale.

def print_scale_value(value):

print (value)

scale = Scale (from_=1 , to=100,width=20,command = print_scale_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()

This will work better.

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

Now, place() is further optimized to another layout manager .grid(column=,row=) .

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 = Canvas( width = ,height = )

canvas.create_image(position_x,position_y, image= )image option in create screenmethod stores the


instance of PhotoImage() class which takes a file location path in file option.

Canvas.create_text(*args , **kwgs) *args include xcor , ycor and **kwargs=text,fill ,font option . fill is for
color,font takes tuple.

You might also like