Lesson-2: Creating a Tkinter window, adding labels
Creating a Tkinter window, adding labels
Creating A Tkinter Window:
import tkinter as tk
pencere=tk.Tk()
pencere.mainloop()
Lesson-2: Creating a Tkinter window, adding labels
Lesson-2: Creating a Tkinter window, adding labels
First, we need to import the tkinter module into our program to start a window. “import tkinter as tk” we can install the tkinter module that we will use in the name of tk with this line of code. Then we need to call the Tk( ) class in the tkinter module. “pencere=tk.Tk()“, we call the Tk( ) class in the tkinter module. I gave him a window as a name. This window is a window that is used to create a window, but it remains on the screen for a very short time and goes away. We can’t detect it. “pencere.mainloop ( )“, we can ensure that this window remains on the screen. A screenshot of the 3-line code that we have written so far is as follows.
Now let’s give our window a title. Then let’s print text in our window with labels.
import tkinter as tk
pencere=tk.Tk()
pencere.title("Python Tkinter Lessons")
etiket1=tk.Label(pencere,text="Welcome...")
etiket1.pack()
etiket2=tk.Label(pencere,text="Button Press")
etiket2.pack()
pencere.mainloop()
pencere.title (“Python Tkinter lessons”) with a line of code, we define our window title, that is, a title. Then etiket1= tk.Label (pencere, text = ” Welcome to the lesson…”) we place a tag named etiket1 in our window with a line of code. But this label will not appear unless we pack it. So we use the etiket1.pack() line of code. In the same way, we follow the same steps for etiket2. After this stage, the screenshot will be as follows.
In this course, we created our first window with the tkinter module. We added a Label and gave our window a title.