26
loading...
This website collects cookies to deliver better user experience
from tkinter import *
window=Tk()
#create a window frame called master
window.title("Checkboxes")
window.geometry("250x100")
label=Label(window,text="text")
#create a label with text 'empty' and put it in the window frame
label.pack() #pack the label into the frame
window.mainloop() #halt execution and display the widgets.
Yesterday we had used the place attribute to place the label in the position. But as the checkbox program has many widgets to be placed, we will use the pack()
method. This method packs the label widget into the frame and the mainloop()
displays them. More abot window.mainloop() here
ChangeLabel
to change the values of the label. For that we make two Boolean attributes, bold and italics. The function must take two Booleans as inputs and must change the value of the label. config()
function. label.config(text='hello')
changes the text value to hello. Similarly we can change the bold or the italics using the font attribute. font=('Helvetica', 18, 'bold')
from tkinter import *
window=Tk()
#create a window frame called master
window.title("Checkboxes")
window.geometry("250x100")
label=Label(window,text="text")
#create a label with text 'empty' and put it in the window frame
def ChangeLabel(bold,italics):
if(bold == True and italics == False):
label.config(text='bold',font=('Helvetica', 18, 'bold'))
elif(bold ==False and italics == True):
label.config(text='italics',font=('Helvetica', 18, 'italic'))
elif(bold ==True and italics == True):
label.config(text='bold and italics',font=('Helvetica', 18, 'bold italic'))
else:
label.config(text='text',font=('Helvetica', 18, ''))
bold= False
italics= True
ChangeLabel(bold,italics)
label.pack()
# The pack() and mainloop() attributes must be at the end of the program
window.mainloop()
Note pack()
and mainloop()
attributes must be at the end of the program, else the program won't function as expected.
Boldcheckbox= Checkbutton(window,text="Bold", variable=bold,onvalue=1, offvalue=0,command=ChangeLabel)
window
: Set the window frame instance into the checkboxtext="Bold"
: Set the text of the checkboxvariable=bold
: give the checkbox a variable to change. When the button is on, the value of the bold will be set 1
and when it is off, the value will be 0
.onvalue=1, offvalue=0
: Set the on and off values.command=ChangeLabel
: The command to be executedBoldcheckbox= Checkbutton(window,text="Bold",........,command=ChangeLabel(bold,italics))
Boldcheckbox= Checkbutton(window,text="Bold", variable=bold,onvalue=True, offvalue=False,command=ChangeLabel)
fails!!BooleanVar()
. The BooleanVar() is an object which returns the Boolean value using the BooleanVar.get()
method and we can set the value using the BooleanVar.set()
method. The BooleanVar represents 1 for true and 0 for false.def ChangeLabel():
if(bold.get() == 1 and italics.get() == 0):
label.config(text='bold',font=('Helvetica', 18, 'bold'))
elif(bold.get() ==0 and italics.get() == 1):
label.config(text='italics',font=('Helvetica', 18, 'italic'))
elif(bold.get() ==1 and italics.get() == 1):
label.config(text='bold and italics',font=('Helvetica', 18, 'bold italic'))
else:
label.config(text='text',font=('Helvetica', 18, ''))
bold= BooleanVar()
italics= BooleanVar()
BooleanVar() and other such objects will be explained in more depth in the next session
from tkinter import *
window=Tk()
#create a window frame called master
window.title("Checkboxes")
window.geometry("250x100")
label=Label(window,text="text")
#create a label with text 'empty' and put it in the window frame
def ChangeLabel():
if(bold.get() == 1 and italics.get() == 0):
label.config(text='bold',font=('Helvetica', 18, 'bold'))
elif(bold.get() ==0 and italics.get() == 1):
label.config(text='italics',font=('Helvetica', 18, 'italic'))
elif(bold.get() ==1 and italics.get() == 1):
label.config(text='bold and italics',font=('Helvetica', 18, 'bold italic'))
else:
label.config(text='text',font=('Helvetica', 18, ''))
bold= BooleanVar()
italics= BooleanVar()
Boldcheckbox= Checkbutton(window,text="Bold", variable= bold, onvalue=1,offvalue=0,command=ChangeLabel)
Italicscheckbox= Checkbutton(window,text="Italics", variable= italics, onvalue=1,offvalue=0,command=ChangeLabel)
label.pack()
Boldcheckbox.pack()
Italicscheckbox.pack()
#pack in the order of appearance.
# The mainloop() attributes must be at the end of the program
window.mainloop()