Lesson-4: Tkinter Button Operations. Button-Clicking.

Tkinter Button Operations. Button-Clicking.

In this course, we will see how to add buttons and click on the button.
Tkinter.we will add buttons using the button () function. We’ll determine what to do if it clicks. First, I suggest you watch the video below.

import tkinter as tk
pencere=tk.Tk()

def degistir():
    yazi.config(text="Yazı değişti...")

yazi=tk.Label(pencere)
yazi.config(text="Tıklarsan yazı değişir.)")
yazi.pack()
dugme=tk.Button(pencere)
dugme.config(text="Yazıyı değiştir")
dugme.config(command=degistir)
dugme.pack()

pencere.mainloop()

The region written in red is the part of adding a button (button). IK.A button is added to the window using the Button command. with config, this added button is edited. The point to note here is the command command. Thanks to this command, it is written what will happen when you click on the button. In the example above, the change function works when you click the button.

def degistir():
    yazi.config(text="Yazı değişti...")

if the function named “degistir” has changed the font label as the”yazı değişti…”

Bu görsel boş bir alt niteliğe sahip; dosya adı 1.jpg

As an example, import the sys module and install sys.if we use the exit() function, we will prepare the exit button from the program. By the way, note that we do not put parentheses when writing functions in config.

Close The Window When The Button Is Clicked

import tkinter as tk
pencere=tk.Tk()


dugme = tk.Button(text="OK", command = pencere.quit)
dugme.pack()
pencere.mainloop()

If you notice, the use of the” Button () “tool is very similar to the use of the” Label ” tool we have seen before. Here we also used some parameters in parentheses. we already know the” text “parameter: we determine the text we want to show to the user with the help of this” text ” parameter. The “command” parameter, which we see in the same parenthesis, shows the command to be executed when you click on the button. We’re here in the window.by giving the” quit ” command, we asked that the window be closed when the button was clicked. So we see three new properties in a row:

Button: the window tool we will use ( the English word “button” means “button” in Turkish). ˘
command: the command to be executed by clicking on the button we created
pencere.quit: a command that allows you to close the window when you click the quit button.

Create A New File By Pressing The Button:

The” command ” parameter mentioned above can make you do very good things. For example, let’s say we want to design an interface that creates a new file on the computer by pressing the “Create” button .

import tkinter as tk
pencere=tk.Tk()

def dosyaYap():
    dosya = open("deneme.txt", "w")

dugme = tk.Button(text = "Dosya Oluştur", command=dosyaYap())
dugme.pack()
pencere.mainloop()

Daha sonra Button() pencere aracı yardımıyla, pencereye yerleştireceğimiz düğmeyi meydana getirdik. Burada “command” parametresine biraz önce oluşturduğumuz fonksiyonu atayarak düğmeye basıldığında yeni bir dosya oluşturulmasına zemin hazırladık. “text” parametresi yardımıyla da düğmemizin adını “Dosya oluştur” olarak belirledik.

Let’s add an exit button to this interface.

import tkinter as tk
pencere=tk.Tk()

def dosyaYap():
    dosya = open("deneme.txt", "w")

dugme = tk.Button(text = "Dosya Oluştur", command=dosyaYap())
dugme.pack()

dugme2 = tk.Button(text = "ÇIKIŞ", command=pencere.quit)
dugme2.pack()

pencere.mainloop()

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *