Giờ là lúc bạn bước ra khỏi thế giới dòng lệnh và xây dựng một ứng dụng có giao diện người dùng thực sự. Trong mini project này, bạn sẽ làm một máy tính cộng trừ nhân chia với giao diện, sử dụng thư viện GUI nổi tiếng nhất trong Python: Tkinter.
Mục tiêu
- Làm quen với giao diện đồ họa (GUI) trong Python
- Hiểu cách tạo cửa sổ, nút bấm, ô nhập
- Xử lý sự kiện khi người dùng nhấn nút
- Xây dựng máy tính đơn giản hỗ trợ
+
,–
,×
,÷
1. Giới thiệu về Tkinter
- Tkinter là thư viện GUI có sẵn trong Python
- Cho phép tạo cửa sổ ứng dụng, nút bấm, ô nhập, menu, v.v
- Cực kỳ dễ tiếp cận với người mới học
2. Cài đặt và khởi tạo cửa sổ
Không cần cài thêm gì! Bạn có thể chạy được luôn nếu dùng Python bản chuẩn.
import tkinter as tk
root = tk.Tk()
root.title("Máy Tính Python")
root.mainloop()
3. Bố cục máy tính đơn giản
Chúng ta sẽ làm:
- Một ô hiển thị biểu thức
- Các nút từ 0–9 và phép toán
- Nút
=
để tính kết quả - Nút
C
để xoá
4. Code hoàn chỉnh máy tính đơn giản
import tkinter as tk
# Biến lưu biểu thức hiện tại
expression = ""
# Hàm xử lý khi bấm nút
def press(num):
global expression
expression += str(num)
input_text.set(expression)
def equal():
global expression
try:
result = str(eval(expression))
input_text.set(result)
expression = result
except:
input_text.set("Lỗi")
expression = ""
def clear():
global expression
expression = ""
input_text.set("")
# Tạo giao diện
root = tk.Tk()
root.title("Máy Tính Python")
input_text = tk.StringVar()
# Ô hiển thị
input_frame = tk.Frame(root, width=400, height=50, bd=0)
input_frame.pack()
input_field = tk.Entry(input_frame, font=('arial', 18), textvariable=input_text, width=50, bd=5, relief='sunken', justify='right')
input_field.grid(row=0, column=0)
input_field.pack(ipady=10)
# Khung chứa nút
btns_frame = tk.Frame(root)
btns_frame.pack()
# Hàng 1
tk.Button(btns_frame, text='C', width=10, height=2, command=clear).grid(row=0, column=0)
tk.Button(btns_frame, text='/', width=10, height=2, command=lambda: press('/')).grid(row=0, column=1)
tk.Button(btns_frame, text='*', width=10, height=2, command=lambda: press('*')).grid(row=0, column=2)
tk.Button(btns_frame, text='-', width=10, height=2, command=lambda: press('-')).grid(row=0, column=3)
# Hàng 2
tk.Button(btns_frame, text='7', width=10, height=2, command=lambda: press(7)).grid(row=1, column=0)
tk.Button(btns_frame, text='8', width=10, height=2, command=lambda: press(8)).grid(row=1, column=1)
tk.Button(btns_frame, text='9', width=10, height=2, command=lambda: press(9)).grid(row=1, column=2)
tk.Button(btns_frame, text='+', width=10, height=2, command=lambda: press('+')).grid(row=1, column=3)
# Hàng 3
tk.Button(btns_frame, text='4', width=10, height=2, command=lambda: press(4)).grid(row=2, column=0)
tk.Button(btns_frame, text='5', width=10, height=2, command=lambda: press(5)).grid(row=2, column=1)
tk.Button(btns_frame, text='6', width=10, height=2, command=lambda: press(6)).grid(row=2, column=2)
tk.Button(btns_frame, text='=', width=10, height=5, command=equal).grid(row=2, column=3, rowspan=2)
# Hàng 4
tk.Button(btns_frame, text='1', width=10, height=2, command=lambda: press(1)).grid(row=3, column=0)
tk.Button(btns_frame, text='2', width=10, height=2, command=lambda: press(2)).grid(row=3, column=1)
tk.Button(btns_frame, text='3', width=10, height=2, command=lambda: press(3)).grid(row=3, column=2)
# Hàng 5
tk.Button(btns_frame, text='0', width=21, height=2, command=lambda: press(0)).grid(row=4, column=0, columnspan=2)
tk.Button(btns_frame, text='.', width=10, height=2, command=lambda: press('.')).grid(row=4, column=2)
root.mainloop()
Bạn vừa học được
- Tạo giao diện với
Tk()
- Tạo ô nhập (
Entry
) và thay đổi nội dung vớiStringVar
- Tạo các
Button
và gán hàm xử lý - Sắp xếp bố cục với
Frame
vàgrid()
- Dùng
eval()
để xử lý biểu thức toán học - Tách rõ giao diện và logic xử lý
Nâng cao (tùy chọn)
- Thêm chức năng
DEL
để xoá 1 ký tự - Hiển thị biểu thức và kết quả trên 2 dòng
- Thêm tổ hợp phím (enter để =, esc để clear)
- Làm giao diện đẹp hơn bằng thư viện
ttk
,customtkinter
, hoặcPyQt
sau này
Dự án này giúp bạn:
- Biết làm ứng dụng mini có giao diện
- Hiểu cách tổ chức giao diện – logic – sự kiện
- Tự tin tạo các app nhỏ như ghi chú, quản lý chi tiêu, trò chơi đơn giản…
Thảo luận