Lesson-9: Tkinter Calculator Interface

Tkinter Calculator Interface

import tkinter as tk
pencere=tk.Tk()

pencere.resizable(width="FALSE",height="FALSE")
liste = ["9", "8", "7","6", "5", "4","3", "2", "1","0", "+", "-","/", "*", "=","C"]

sira = 1
sutun = 0

for i in liste:
    tk.Button(text=i,width=4).grid(row=sira,column=sutun)
    sutun += 1
    if sutun > 2:
        sutun = 0
        sira += 1


pencere.mainloop()

First, we made the resizable property FALSE so that the window cannot be changed. We created a list called list and added all the button messages that I would use in the calculator. We need to adjust the ROW and COLUMN values well to make the buttons appear on the screen regularly. So we defined two variables named sira and Sira and assigned values sira=1, Sira=0.

Now it’s time to place the buttons in the window. It can be done by adding one by one, but we can also easily do it in the for cycle. For loops, as we saw in python lessons, could iterate on lists, that is, move around. In this way, we open the for loop on the list named list and navigate through the elements in the list one by one.

tk.Button(text=i,width=4).grid(row=sira,column=sutun)

This line of code adds the button to the window. If you notice, we used the grid command in the same line. The important point is the row and column values. After each addition, the value of the column is increased by 1 with “sutun+=1“. Immediately below it, if the column has passed 2, it is reset. The row variable must also be increased by 1 when the column is reset. Because we’ll have to get to the bottom line. This loop will rotate up to the number of elements in the list, and a button will be added to the window in each loop.

SCREEN SHOT:

tkinter hesap makinası
Lesson-9: Tkinter Calculator Interface

You may also like...

Leave a Reply

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