More tkinter : Combine buttons and labels.

Now that we can add a label and a button, it is time to combine the two. The following code does this.

#define functions for button(s)
def btn1():
	print ("button pressed")
	
	
#create button object	
btn_tog2 = Button( window, text ='button1', command=btn1)
btn_exit = Button( window, text ='exit',command=exit)	

#place button object
btn_tog2.grid(row = 1, column = 2, padx = 5, pady = 5)
btn_exit.grid(row = 2, column = 2, padx = 5, pady = 5)

#define labeles
button1 = Label(window, text="press button")
button2 = Label(window, text="exit program")

#place labels
button1.grid(row = 1, column = 1, padx = 5, pady = 5)
button2.grid(row = 2, column = 1, padx = 5, pady = 5)

We get:

tk label

#tkinter,#python,#gui,#programming.


MastodonPeertubeJoin Mastodon

AI statement : Consent is NOT granted to use the content of this blog for the purposes of AI training or similar activity. Consent CANNOT be assumed, it has to be granted.

Donate using Liberapay