ในบทช่วยสอนนี้ เราจะสร้างเครื่องคิดเลข GUI 简单โดยใช้โมดูล Tkinter Tkinter เป็นโมดูล built-in ของ Python สำหรับพัฒนาแอปพลิเคชัน GUI มันใช้งานง่ายและมาพร้อมกับ Python เราสามารถแสดงข้อมูลด้วยแอปพลิเคชัน GUI ได้
มาดูวิธีสร้างเครื่องคิดเลข GUI 简单
นำเข้า everything จาก Tkinter โดยใช้ *
สร้างอินเทอร์เฟซสำหรับเครื่องคิดเลข
สร้างฟังก์ชัน input_number ที่ใส่ตัวเลขลงในช่องอินพุต
สร้างฟังก์ชัน clear_input_field ที่ล้างทุกอย่างในช่องอินพุต
และสุดท้าย ฟังก์ชัน evaluate ที่คำนวณและให้ผลลัพธ์ของนิพจน์
ตัวอย่าง
# นำเข้า everything จาก tkinter
from tkinter import *
# นิพจน์เพื่อเข้าถึงระหว่างฟังก์ชันทั้งหมด
expression = ""
# ฟังก์ชัน
def input_number(number, equation):
# เข้าถึงตัวแปร expression ทั่วไป
global expression
# การต่อสตริง
expression = expression + str(number)
equation.set(expression)
def clear_input_field(equation):
global expression
expression = ""
# ตั้งค่าสตริงว่างในช่องอินพุต
equation.set("Enter the expression")
def evaluate(equation):
global expression
# ลองประเมินนิพจน์
try:
result = str(eval(expression))
# แสดงผลลัพธ์ในช่องอินพุต
equation.set(result)
# ตั้งค่า expression เป็นสตริงว่าง
expression = ""
except:
# เกิดข้อผิดพลาดบางอย่าง
# แสดงให้ผู้ใช้เห็น
equation.set("Enter a valid expression")
expression = ""
# สร้าง GUI
def main():
# หน้าต่างหลัก
window = Tk()
# ตั้งค่าชื่อ GUI window
window.title("Calculator")
# ตั้งค่าคอนฟิกของ GUI window
window.geometry("325x175")
# สร้างตัวแปรคลาส
equation = StringVar()
# ช่องอินพุตสำหรับนิพจน์
input_field = Entry(window, textvariable=equation)
input_field.place(height=100)
# เราใช้ตำแหน่ง grid
# สำหรับการจัดวางวิดเจ็ต
input_field.grid(columnspan=4, ipadx=100, ipady=5)
# ตั้งค่าข้อความ placeholder ให้ผู้ใช้
equation.set("Enter the expression")
# สร้างปุ่มและวางตำแหน่งตามลำดับ
_1 = Button(window, text='1', fg='white', bg='black', bd=0, command=lambda: input_number(1, equation), height=2, width=7)
_1.grid(row=2, column=0)
_2 = Button(window, text='2', fg='white', bg='black', bd=0, command=lambda: input_number(2, equation), height=2, width=7)
_2.grid(row=2, column=1)
_3 = Button(window, text='3', fg='white', bg='black', bd=0, command=lambda: input_number(3, equation), height=2, width=7)
_3.grid(row=2, column=2)
_4 = Button(window, text='4', fg='white', bg='black', bd=0, command=lambda: input_number(4, equation), height=2, width=7)
_4.grid(row=3, column=0)
_5 = Button(window, text='5', fg='white', bg='black', bd=0, command=lambda: input_number(5, equation), height=2, width=7)
_5.grid(row=3, column=1)
_6 = Button(window, text='6', fg='white', bg='black', bd=0, command=lambda: input_number(6, equation), height=2, width=7)
_6.grid(row=3, column=2)
_7 = Button(window, text='7', fg='white', bg='black', bd=0, command=lambda: input_number(7, equation), height=2, width=7)
_7.grid(row=4, column=0)
_8 = Button(window, text='8', fg='white', bg='black', bd=0, command=lambda: input_number(8, equation), height=2, width=7)
_8.grid(row=4, column=1)
_9 = Button(window, text='9', fg='white', bg='black', bd=0, command=lambda: input_number(9, equation), height=2, width=7)
_9.grid(row=4, column=2)
_0 = Button(window, text='0', fg='white', bg='black', bd=0, command=lambda: input_number(0, equation), height=2, width=7)
_0.grid(row=5, column=0)
plus = Button(window, text='+', fg='white', bg='black', bd=0, command=lambda: input_number('+', equation), height=2, width=7)
plus.grid(row=2, column=3)
minus = Button(window, text='-', fg='white', bg='black', bd=0, command=lambda: input_number('-', equation), height=2, width=7)
minus.grid(row=3, column=3)
multiply = Button(window, text='*', fg='white', bg='black', bd=0, command=lambda: input_number('*', equation), height=2, width=7)
multiply.grid(row=4, column=3)
divide = Button(window, text='/', fg='white', bg='black', bd=0, command=lambda: input_number('/', equation), height=2, width=7)
divide.grid(row=5, column=3)
equal = Button(window, text='=', fg='white', bg='black', bd=0, command=lambda: evaluate(equation), height=2, width=7)
equal.grid(row=5, column=2)
clear = Button(window, text='Clear', fg='white', bg='black', bd=0, command=lambda: clear_input_field(equation), height=2, width=7)
clear.grid(row=5, column=1)
# แสดง GUI
window.mainloop()
# เริ่มต้นโปรแกรม
if __name__ == '__main__':
main()
ผลลัพธ์
ถ้าคุณรันโปรแกรมข้างต้น คุณจะได้เครื่องคิดเลข简单ดังนี้

ผลลัพธ์ของนิพจน์ข้างต้นหลังกดปุ่ม =

สรุป
หากคุณมีข้อสงสัยในบทช่วยสอนนี้ ให้ระบุในส่วนความคิดเห็น