Apakah Anda mencari cara untuk membuat kalkulator GUI dengan Python menggunakan Tkinter? . Dalam posting blog ini, kita akan membahas langkah-langkah untuk membuat kalkulator dasar dengan Python yang memiliki Graphical User Interface (GUI). Dengan Tkinter, Anda dapat dengan mudah membuat kalkulator sederhana dengan semua operasi matematika dasar seperti penjumlahan, pengurangan, perkalian, dan pembagian. Kami juga akan membahas cara menyesuaikan GUI agar lebih menarik secara visual dan ramah pengguna
Apa itu Tkinter?
Tkinter adalah pustaka Python yang menyediakan akses ke berbagai elemen antarmuka pengguna grafis (GUI). Ini digunakan untuk membangun aplikasi GUI dengan Python dan ini adalah paket GUI standar untuk Python. Tkinter telah ada sejak awal Python, dan sangat berpengaruh dalam pengembangan pemrograman GUI modern. Tkinter menyediakan antarmuka berorientasi objek yang kuat ke toolkit Tcl/Tk GUI. Dengan Tkinter, Anda dapat membuat berbagai macam aplikasi desktop yang tampak hebat dan terasa alami
Baca postingan ini jika ingin mempelajari dasar-dasar perpustakaan Tkinter. Cara Membuat Aplikasi Desktop Menggunakan Python
Baca juga. Dasar-dasar pemrograman python dalam 30 hari
Cara membuat kalkulator GUI dengan Python menggunakan Tkinter
Desain Aplikasi Kalkulator
Sebelum kita memulai pengkodean, pertama-tama kita perlu merancang GUI kalkulator. Untuk memastikan apa sebenarnya yang ingin kita capai. Untuk tutorial ini, kalkulator kita akan terlihat seperti di bawah ini
Oke, sekarang kita tahu apa yang ingin kita capai. Sekarang mari kita mulai pengkodean kita. Saya akan membagi seluruh kode menjadi beberapa langkah untuk membangun kalkulator dasar kita dengan Python dengan pustaka Tkinter
Langkah 1. Perpustakaan Impor & Jendela Aplikasi Pengaturan
Untuk tutorial ini, saya akan mengatur ukuran jendela aplikasi kita. lebar = 312 piksel dan tinggi = 324 piksel
import tkinter as tk # Create app window win = tk.Tk() # Specify screen size of the Application window win.geometry("312x324") # Disable resizing the app window win.resizable(0, 0) # Specify application name win.title("Calculator")
Langkah 2. Tentukan Fungsi
Pada langkah ini, kami akan menulis semua fungsi yang diperlukan untuk proyek kalkulator kami
################### python code for calculator gui #################### input_value = "" # Intialize StringVar display_text = tk.StringVar() # Function to continuously updates input field whenever you click a button def click_button_action(item): global input_value input_value = input_value + str(item) display_text.set(input_value) # Function to clear the display def clear_button_action(): global input_value input_value = "" display_text.set("") # Function to calculate input values def equal_button_action(): global input_value result = str(eval(input_value)) display_text.set(result) input_value = "" _
Dalam kode ini kami membuat tiga fungsi
- klik_tombol_aksi. Fungsi ini untuk menampilkan nilai ke layar
- clear_button_action. Fungsi ini untuk membersihkan layar
- equal_button_action. Fungsi ini untuk menghitung dan menampilkan hasilnya
Di sini, di kode ini
- Di baris 6. Kami sedang menginisialisasi objek ################### python code for calculator gui #################### input_value = "" # Intialize StringVar display_text = tk.StringVar() # Function to continuously updates input field whenever you click a button def click_button_action(item): global input_value input_value = input_value + str(item) display_text.set(input_value) # Function to clear the display def clear_button_action(): global input_value input_value = "" display_text.set("") # Function to calculate input values def equal_button_action(): global input_value result = str(eval(input_value)) display_text.set(result) input_value = "" 2. Tkinter ################### python code for calculator gui #################### input_value = "" # Intialize StringVar display_text = tk.StringVar() # Function to continuously updates input field whenever you click a button def click_button_action(item): global input_value input_value = input_value + str(item) display_text.set(input_value) # Function to clear the display def clear_button_action(): global input_value input_value = "" display_text.set("") # Function to calculate input values def equal_button_action(): global input_value result = str(eval(input_value)) display_text.set(result) input_value = "" _2memungkinkan Anda mengelola nilai widget secara efektif seperti ################### python code for calculator gui #################### input_value = "" # Intialize StringVar display_text = tk.StringVar() # Function to continuously updates input field whenever you click a button def click_button_action(item): global input_value input_value = input_value + str(item) display_text.set(input_value) # Function to clear the display def clear_button_action(): global input_value input_value = "" display_text.set("") # Function to calculate input values def equal_button_action(): global input_value result = str(eval(input_value)) display_text.set(result) input_value = "" 4 atau ################### python code for calculator gui #################### input_value = "" # Intialize StringVar display_text = tk.StringVar() # Function to continuously updates input field whenever you click a button def click_button_action(item): global input_value input_value = input_value + str(item) display_text.set(input_value) # Function to clear the display def clear_button_action(): global input_value input_value = "" display_text.set("") # Function to calculate input values def equal_button_action(): global input_value result = str(eval(input_value)) display_text.set(result) input_value = "" 5
- Baris 23. Kami menghitung output dari input string menggunakan fungsi ################### python code for calculator gui ####################
input_value = ""
# Intialize StringVar
display_text = tk.StringVar()
# Function to continuously updates input field whenever you click a button
def click_button_action(item):
global input_value
input_value = input_value + str(item)
display_text.set(input_value)
# Function to clear the display
def clear_button_action():
global input_value
input_value = ""
display_text.set("")
# Function to calculate input values
def equal_button_action():
global input_value
result = str(eval(input_value))
display_text.set(result)
input_value = ""
6
- Misalnya. ################### python code for calculator gui #################### input_value = "" # Intialize StringVar display_text = tk.StringVar() # Function to continuously updates input field whenever you click a button def click_button_action(item): global input_value input_value = input_value + str(item) display_text.set(input_value) # Function to clear the display def clear_button_action(): global input_value input_value = "" display_text.set("") # Function to calculate input values def equal_button_action(): global input_value result = str(eval(input_value)) display_text.set(result) input_value = "" 7 => ################### python code for calculator gui #################### input_value = "" # Intialize StringVar display_text = tk.StringVar() # Function to continuously updates input field whenever you click a button def click_button_action(item): global input_value input_value = input_value + str(item) display_text.set(input_value) # Function to clear the display def clear_button_action(): global input_value input_value = "" display_text.set("") # Function to calculate input values def equal_button_action(): global input_value result = str(eval(input_value)) display_text.set(result) input_value = "" 8 (keluaran)
Langkah3. Menambahkan Widget
Sekarang kita perlu menambahkan semua baris widget yang diperlukan untuk membuat antarmuka grafis untuk aplikasi kalkulator kita. Pertama, kita perlu membagi seluruh jendela menjadi dua bingkai, lalu kita perlu menambahkan widget di dalam kedua bingkai tersebut
- Bingkai Tampilan
- Widget => kolom input (################### python code for calculator gui #################### input_value = "" # Intialize StringVar display_text = tk.StringVar() # Function to continuously updates input field whenever you click a button def click_button_action(item): global input_value input_value = input_value + str(item) display_text.set(input_value) # Function to clear the display def clear_button_action(): global input_value input_value = "" display_text.set("") # Function to calculate input values def equal_button_action(): global input_value result = str(eval(input_value)) display_text.set(result) input_value = "" 5 widget)
- Bingkai tombol
- Dalam bingkai ini, semua tombol angka dan ekspresi akan ada di sana
- Baris ke-1. # Create a frame for the display field input_frame = tk.Frame(win, width=312, height=50, bd=0, highlightbackground="black", highlightcolor="black", highlightthickness=2) input_frame.pack(side = tk.TOP) _0(kombinasi 3 kolom pertama) dan # Create a frame for the display field input_frame = tk.Frame(win, width=312, height=50, bd=0, highlightbackground="black", highlightcolor="black", highlightthickness=2) input_frame.pack(side = tk.TOP) 1 (/)
- baris ke-2. # Create a frame for the display field input_frame = tk.Frame(win, width=312, height=50, bd=0, highlightbackground="black", highlightcolor="black", highlightthickness=2) input_frame.pack(side = tk.TOP) 2, dan # Create a frame for the display field input_frame = tk.Frame(win, width=312, height=50, bd=0, highlightbackground="black", highlightcolor="black", highlightthickness=2) input_frame.pack(side = tk.TOP) 3 (X)
- baris ke-3. # Create a frame for the display field input_frame = tk.Frame(win, width=312, height=50, bd=0, highlightbackground="black", highlightcolor="black", highlightthickness=2) input_frame.pack(side = tk.TOP) 4, dan # Create a frame for the display field input_frame = tk.Frame(win, width=312, height=50, bd=0, highlightbackground="black", highlightcolor="black", highlightthickness=2) input_frame.pack(side = tk.TOP) 5 (-)
- baris ke-4. # Create a frame for the display field input_frame = tk.Frame(win, width=312, height=50, bd=0, highlightbackground="black", highlightcolor="black", highlightthickness=2) input_frame.pack(side = tk.TOP) 6, dan # Create a frame for the display field input_frame = tk.Frame(win, width=312, height=50, bd=0, highlightbackground="black", highlightcolor="black", highlightthickness=2) input_frame.pack(side = tk.TOP) 7 (+)
- baris ke-5. # Create a frame for the display field input_frame = tk.Frame(win, width=312, height=50, bd=0, highlightbackground="black", highlightcolor="black", highlightthickness=2) input_frame.pack(side = tk.TOP) _8 (kombinasi dari dua kolom pertama), # Create a frame for the display field input_frame = tk.Frame(win, width=312, height=50, bd=0, highlightbackground="black", highlightcolor="black", highlightthickness=2) input_frame.pack(side = tk.TOP) 9 (. ) dan # Create a input field inside the 'Frame' input_field = tk.Entry(input_frame, font=('arial', 18, 'bold'), textvariable = display_text, width=50, bg="#eee", bd=0, justify=tk.RIGHT) input_field.grid(row=0, column=0) input_field.pack(ipady=10) # 'ipady' is for internal padding to change the height of the input field 0 (=)
Baca Juga. Flask vs Django. Mana yang Lebih Mudah untuk Machine Learning?
Oke sekarang, mari kita mulai mendesain kalkulator GUI kita dengan fungsionalitas
1. Buat bingkai Tampilan
Mari buat bingkai untuk menampilkan input
# Create a frame for the display field input_frame = tk.Frame(win, width=312, height=50, bd=0, highlightbackground="black", highlightcolor="black", highlightthickness=2) input_frame.pack(side = tk.TOP)
Di sini # Create a input field inside the 'Frame' input_field = tk.Entry(input_frame, font=('arial', 18, 'bold'), textvariable = display_text, width=50, bg="#eee", bd=0, justify=tk.RIGHT) input_field.grid(row=0, column=0) input_field.pack(ipady=10) # 'ipady' is for internal padding to change the height of the input field _1 adalah lebar batas
1. 1 Buat widget EntriWidget ################### python code for calculator gui #################### input_value = "" # Intialize StringVar display_text = tk.StringVar() # Function to continuously updates input field whenever you click a button def click_button_action(item): global input_value input_value = input_value + str(item) display_text.set(input_value) # Function to clear the display def clear_button_action(): global input_value input_value = "" display_text.set("") # Function to calculate input values def equal_button_action(): global input_value result = str(eval(input_value)) display_text.set(result) input_value = "" 5 di Tkinter digunakan untuk memasukkan atau menampilkan satu baris teks. Mari kita buat itu
# Create a input field inside the 'Frame' input_field = tk.Entry(input_frame, font=('arial', 18, 'bold'), textvariable = display_text, width=50, bg="#eee", bd=0, justify=tk.RIGHT) input_field.grid(row=0, column=0) input_field.pack(ipady=10) # 'ipady' is for internal padding to change the height of the input field
2. Buat Bingkai untuk tombol
Sekarang mari buat bingkai lain untuk tombol di bawah # Create a input field inside the 'Frame' input_field = tk.Entry(input_frame, font=('arial', 18, 'bold'), textvariable = display_text, width=50, bg="#eee", bd=0, justify=tk.RIGHT) input_field.grid(row=0, column=0) input_field.pack(ipady=10) # 'ipady' is for internal padding to change the height of the input field 3 atau # Create a input field inside the 'Frame' input_field = tk.Entry(input_frame, font=('arial', 18, 'bold'), textvariable = display_text, width=50, bg="#eee", bd=0, justify=tk.RIGHT) input_field.grid(row=0, column=0) input_field.pack(ipady=10) # 'ipady' is for internal padding to change the height of the input field 4
# Create another 'Frame' for buttons btns_frame = tk.Frame(win, width=312, height=272.5, bg="grey") btns_frame.pack()
Jadi, kami membuat bingkai tombol kami. Sekarang mari buat semua widget tombol yang diperlukan baris demi baris
2. 1 Buat baris PertamaDi baris ini, seharusnya hanya ada ________20______5 dan # Create a frame for the display field input_frame = tk.Frame(win, width=312, height=50, bd=0, highlightbackground="black", highlightcolor="black", highlightthickness=2) input_frame.pack(side = tk.TOP) 1
# 1st row clear_btn = tk.Button(btns_frame, text = "C", fg = "black", width = 32, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: clear_button_action()).grid(row = 0, column = 0, columnspan = 3, padx = 1, pady = 1) divide_btn = tk.Button(btns_frame, text = "/", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: click_button_action("/")).grid(row = 0, column = 3, padx = 1, pady = 1)
Di baris ini, harus ada empat tombol
- Tombol untuk angka 7
- Tombol untuk angka 8
- Tombol untuk angka 9
- Tombol untuk perkalian (x)
# 2nd row btn_7 = tk.Button(btns_frame, text = "7", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: click_button_action(7)).grid(row = 1, column = 0, padx = 1, pady = 1) btn_8 = tk.Button(btns_frame, text = "8", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: click_button_action(8)).grid(row = 1, column = 1, padx = 1, pady = 1) btn_9 = tk.Button(btns_frame, text = "9", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: click_button_action(9)).grid(row = 1, column = 2, padx = 1, pady = 1) multiply_btn = tk.Button(btns_frame, text = "X", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: click_button_action("*")).grid(row = 1, column = 3, padx = 1, pady = 1)
2. 3 Buat baris ke-3Di baris ini, akan ada empat tombol
- Tombol untuk angka 4
- Tombol untuk angka 5
- Tombol untuk angka 6
- Tombol untuk Minus atau pengurangan (-)
# 3rd row btn_4 = tk.Button(btns_frame, text = "4", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: click_button_action(4)).grid(row = 2, column = 0, padx = 1, pady = 1) btn_5 = tk.Button(btns_frame, text = "5", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: click_button_action(5)).grid(row = 2, column = 1, padx = 1, pady = 1) btn_6 = tk.Button(btns_frame, text = "6", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: click_button_action(6)).grid(row = 2, column = 2, padx = 1, pady = 1) minus_btn = tk.Button(btns_frame, text = "-", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: click_button_action("-")).grid(row = 2, column = 3, padx = 1, pady = 1)
2. 4 Buat baris ke-4Di baris ini, empat tombol harus tersedia
- Tombol untuk angka 1
- Tombol untuk angka 2
- Tombol untuk angka 3
- Tombol untuk plus atau tambahan (+)
# 4th row btn_1 = tk.Button(btns_frame, text = "1", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: click_button_action(1)).grid(row = 3, column = 0, padx = 1, pady = 1) btn_2 = tk.Button(btns_frame, text = "2", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: click_button_action(2)).grid(row = 3, column = 1, padx = 1, pady = 1) btn_3 = tk.Button(btns_frame, text = "3", fg = "black", width = 10, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: click_button_action(3)).grid(row = 3, column = 2, padx = 1, pady = 1) plus_btn = tk.Button(btns_frame, text = "+", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: click_button_action("+")).grid(row = 3, column = 3, padx = 1, pady = 1)
2. 5 Buat Baris ke-5Di baris ini, tiga yang terakhir harus ada
- Tombol untuk angka 0
- Tombol untuk titik (. )
- tombol sama (=)
# 5th row btn_0 = tk.Button(btns_frame, text = "0", fg = "black", width = 21, height = 3, bd = 0, bg = "#fff", cursor = "hand2", command = lambda: click_button_action(0)).grid(row = 4, column = 0, columnspan = 2, padx = 1, pady = 1) point_btn = tk.Button(btns_frame, text = ".", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: click_button_action(".")).grid(row = 4, column = 2, padx = 1, pady = 1) equals_btn = tk.Button(btns_frame, text = "=", fg = "black", width = 10, height = 3, bd = 0, bg = "#eee", cursor = "hand2", command = lambda: equal_button_action()).grid(row = 4, column = 3, padx = 1, pady = 1)
Langkah4. Jalankan mainloop()
Pada titik ini, kita selesai dengan kode python kita untuk GUI kalkulator. Sekarang kita hanya perlu menjalankan metode # Create a input field inside the 'Frame' input_field = tk.Entry(input_frame, font=('arial', 18, 'bold'), textvariable = display_text, width=50, bg="#eee", bd=0, justify=tk.RIGHT) input_field.grid(row=0, column=0) input_field.pack(ipady=10) # 'ipady' is for internal padding to change the height of the input field 7 untuk melihat aplikasi secara langsung
################### python code for calculator gui #################### input_value = "" # Intialize StringVar display_text = tk.StringVar() # Function to continuously updates input field whenever you click a button def click_button_action(item): global input_value input_value = input_value + str(item) display_text.set(input_value) # Function to clear the display def clear_button_action(): global input_value input_value = "" display_text.set("") # Function to calculate input values def equal_button_action(): global input_value result = str(eval(input_value)) display_text.set(result) input_value = "" _0Itu dia. Anda berhasil membuat kalkulator dengan python menggunakan perpustakaan Tkinter, yang seharusnya sudah aktif dan berjalan di sistem windows Anda sekarang
Baca Juga. Unggah dan tampilkan gambar di Flask Python
Kode Lengkap
Saya membahas banyak hal untuk membuat GUI kalkulator Python. Izinkan saya sekarang mengumpulkan semuanya dan membuat kode akhir untuk Anda
################### python code for calculator gui #################### input_value = "" # Intialize StringVar display_text = tk.StringVar() # Function to continuously updates input field whenever you click a button def click_button_action(item): global input_value input_value = input_value + str(item) display_text.set(input_value) # Function to clear the display def clear_button_action(): global input_value input_value = "" display_text.set("") # Function to calculate input values def equal_button_action(): global input_value result = str(eval(input_value)) display_text.set(result) input_value = "" _1
Catatan akhir
Dalam tutorial ini, saya membuat proyek kalkulator dengan Python menggunakan pustaka Tkinter. Sekarang, ini hanyalah titik awal, Anda dapat melanjutkan ini untuk membuat proyek GUI kalkulator tingkat lanjut seperti kalkulator ilmiah
Baca Juga. Panduan pemula untuk framework Flask Python Web App
Ini semua untuk tutorial ini, jika Anda memiliki pertanyaan atau saran mengenai posting ini, tulis di bagian komentar di bawah
Anindya
Halo, saya Anindya Naskar, Insinyur Ilmu Data. Saya membuat situs web ini untuk menunjukkan kepada Anda apa yang saya yakini sebagai cara terbaik untuk memulai di bidang Ilmu Data