To add a button in tkinter, use the Button class, and the syntax is shown below:
Syntax
bot = Button (window, text = "Click here")
bot.grid (column = 1, row = 0)
So the complete code will look like this:
from tkinter import *
window = Tk()
window. title("Tkinter Button")
window.geometry ('250x250')
label = Label(window, text="First button")
label.grid(column=0, row=0)
button = Button(window, text = "Click here")
button.grid(column=1, row=0)
window.mainloop()
Notice that we put the button in the first column of the window, which is 0. This because the button after being created will need to be placed somewhere in the window, or it will not appear on the screen.
Tkinter button - how to change the colors of a button and the background color of the button
You can change the color of a button or any other widget using the fg property. In addition, you can change the background color of any widget using the bg property.
bot = Button(window, text = "Click here", bg = "black", fg = "white")
So the complete code will look like this:
from tkinter import *
window = Tk ()
window. title ("Tkinter Button")
window.geometry ('250x250')
label = Label(window, text="First button")
label.grid(column=0, row=0)
button = Button (window, text = "Click here", bg = "black", fg = "white")
button.grid(column=1, row=0)
window.mainloop ()
Python Tkinter button - how to change size of the button
We can also change the width and the height of a Button, see the bellow code to see how it works.
from tkinter import *
window = Tk()
window. title("Tkinter Button")
window.geometry('250x250')
label = Label(window, text="First button")
label.grid(column=0, row=0)
button = Button(window, text = "Click here", width=10, height=2, bg = "black", fg = "white")
button.grid(column=0, row=1)
window.mainloop()
Now, if you tried to click the button, nothing happens because the button's click event has not yet been recorded.
Python Tkinter - Click event on button
First, we write the function we need to execute when the button is clicked:
def hello():
print("Hello World, I'm ProgrammindgRoad ' !! ")
label.configure (text = "Hello World, I'm ProgrammindgRoad ' !! ")
And then we will connect the function we just created with the button, specifying the function as follows:
button = Button (window, text = "Click here", width=10, height=2, bg = "black", fg = "white", command = hello)
button.grid(column=0, row=1)
Here, the command attribute , serves to pass functions or events to the button, in this case we pass the function hello, which prints the message 'Hello World, I'm ProgrammindgRoad'.
Note that, we type hello just not hello() in parentheses.
As soon as you run the program, and click the button, you will see that the message will be printed on your terminal.
So the complete code will look like this:
from tkinter import *
window = Tk()
window. title("Tkinter Button")
window.geometry('250x250')
def hello():
print("Hello World, I'm ProgrammindgRoad ' !! ")
label.configure (text = "Hello World, I'm ProgrammindgRoad ' !! ")
label = Label(window, text="First button")
label.grid(column=0, row=0)
button = Button(window, text = "Click here", width=10, height=2, bg = "black", fg = "white", command = hello)
button.grid(column=0, row=1)
window.mainloop()
Post a Comment
0Comments