Làm thế nào để bạn tạo một máy tính phức tạp trong python?

Tuy nhiên, trong trường hợp viết một chương trình tính toán đơn giản, có nhiều cách khác nhau để thực hiện. Cái nào là tốt nhất, phụ thuộc rất nhiều vào ngữ cảnh

Trong bài viết này, tôi sẽ thảo luận về ba cách tiếp cận phổ biến, bao gồm điểm mạnh và điểm yếu của từng cách tiếp cận.

Giải pháp 1. Phân nhánh có điều kiện

Tôi đoán nếu có một cách "rõ ràng" duy nhất để xây dựng một máy tính trong Python, thì đó sẽ là sử dụng phân nhánh có điều kiện

Các bước cơ bản là

  1. Yêu cầu người dùng nhập dữ liệu
  2. Chọn một thao tác có điều kiện dựa trên toán tử đã chọn
  3. Thực hiện phép tính bằng toán tử đã chọn
  4. In kết quả

Mã sẽ trông giống như thế này

Không có gì sai với cách tiếp cận này và trên thực tế, hầu hết các hướng dẫn đều chọn một số biến thể của cách này. Nhưng nhược điểm bao gồm

  • Chỉ hỗ trợ hoạt động nhị phân
  • Khá nhiều mã cho một nhiệm vụ đơn giản

Có thể có các giải pháp tốt hơn tùy thuộc vào những gì bạn đang cố gắng đạt được

Đây là một liên kết đến mã làm việc

Giải pháp 2. đánh giá[]

Một cách tiếp cận khác là sử dụng hàm eval[] tích hợp sẵn của Python. eval cho phép bất kỳ chuỗi nào được đánh giá dưới dạng biểu thức Python. Điều này cực kỳ mạnh mẽ vì nó cho phép mã được tạo và thực thi động khi chạy

Sử dụng điều này, đoạn mã trên có thể được giảm xuống

calc = input["Type calculation:\n"]

print["Answer: " + str[eval[calc]]]

eval[] có một số ưu điểm so với giải pháp 1

  • Nó có thể chấp nhận các biểu thức tùy ý [e. g. 1+2-3*4] không chỉ hoạt động nhị phân
  • Đó là mã ít hơn đáng kể

Đây là một liên kết đến mã làm việc

eval có thể là “một cách rõ ràng” nếu bạn chỉ sử dụng nó cục bộ trên máy tính của mình, vì nó rất tiện lợi và mạnh mẽ. Tuy nhiên, nó nên được sử dụng hết sức cẩn thận trong môi trường sản xuất. Lý do là vì eval có thể thực thi các biểu thức python tùy ý, vì vậy có thể được sử dụng cho mục đích xấu

Bạn có thể tìm thấy một bài viết hay về rủi ro bảo mật của eval tại đây

Giải pháp 3. Tra cứu từ điển và đệ quy

Trong phiên bản máy tính Python này, chúng tôi sẽ thực hiện các cải tiến sau so với giải pháp 1

  • Tra cứu từ điển được sử dụng để xác định nhánh có điều kiện. Điều này làm cho mã sạch hơn, dễ đọc hơn và có thể mở rộng hơn
  • Mô-đun
    left = '1'
    operator = '+'
    right = '2*3'
    3 của Python được sử dụng thay vì xác định các hàm toán học của riêng bạn
  • Đệ quy được sử dụng để cho phép hỗ trợ các biểu thức toán học phức tạp [không chỉ các phép toán nhị phân]

Mã kết quả như sau

Cách tiếp cận này mạnh hơn giải pháp 1 và an toàn hơn giải pháp 2

Cách nó hoạt động là khi người dùng chuyển vào một chuỗi [e. g. 1+2*3], hàm

left = '1'
operator = '+'
right = '2*3'
4 của chúng ta lặp lại trên mỗi toán tử mà chúng ta đã định nghĩa trong từ điển
left = '1'
operator = '+'
right = '2*3'
5 và gọi phương thức phân vùng chuỗi trong lần xuất hiện đầu tiên của toán tử đó. Trong trường hợp của
left = '1'
operator = '+'
right = '2*3'
6, điều này sẽ cho chúng ta

left = '1'
operator = '+'
right = '2*3'

left = '1'
operator = '+'
right = '2*3'
4 sau đó được gọi đệ quy trên các biểu thức con cho đến khi nó
left = '1'
operator = '+'
right = '2*3'
8 trả về true. Vì vậy, trong ví dụ trên, lệnh gọi đệ quy của chúng ta sẽ như thế này

________số 8_______

Mà giải quyết để

return add[1, 6]

Nếu tôi đang viết một máy tính “thế giới thực” bằng Python, tôi có thể lấy thứ gì đó như thế này làm điểm xuất phát của mình

Đây là một liên kết đến mã làm việc

giải pháp tiền thưởng. mô-đun điện tử

Cách tiếp cận “phần thưởng” cuối cùng và có thể là cách tiếp cận yêu thích của tôi là sử dụng mô-đun e tuyệt vời. Với điều này, chúng ta có thể đánh giá các biểu thức Python “trực tiếp” từ dòng lệnh mà không cần mở trình thông dịch Python

Để cài đặt

pip install e

Sau đó, để sử dụng, chúng ta chỉ cần viết

$ python -me 1 + 1
2
$ python -me "10 * 5 - [6**2] + 6 / 3"
16

Rất tiện dụng

Tôi hy vọng bạn thấy bài viết này hữu ích. Nếu bạn có bất kỳ ý tưởng nào khác để triển khai máy tính Pythonic, tôi rất muốn nghe về ý tưởng đó trong các nhận xét bên dưới

Tăng cấp mã hóa

Cảm ơn vì đã là một phần của cộng đồng của chúng tôi. Đăng ký kênh YouTube của chúng tôi hoặc tham gia Skilled. khóa học phỏng vấn lập trình dev

Ai không cần một máy tính trong thời đại này? . Hôm nay chúng ta sẽ tìm hiểu về cách xây dựng Máy tính khoa học bằng Python. Có một máy tính khoa học là điều bắt buộc phải có khi nói đến các yêu cầu hàng ngày của sinh viên khoa học. Có một máy tính khoa học trên màn hình của bạn sẽ hỗ trợ rất nhiều cho việc hoàn thành các dự án trực tuyến, đặc biệt là trong thời đại học trực tuyến hiện nay. để sao chép mã nguồn

Bạn đã bao giờ tự hỏi về việc phát triển Máy tính khoa học GUI bằng Python chưa? . Ở đây trong bài viết này, chúng tôi sẽ đưa bạn qua tất cả các khái niệm quan trọng và cũng cung cấp cho bạn mã nguồn để bạn tham khảo

Mục lục

Máy tính khoa học trong Python. Tổng quan dự án

Tên dự án. Máy tính khoa học trong Python sử dụng TkinterAbstractĐó là một dự án dựa trên GUI được sử dụng với mô-đun Tkinter để sắp xếp tất cả các phần tử hoạt động trong Máy tính khoa học. Ngôn ngữ sử dụng. PythonIDEThonny và PyCharm[Được khuyến nghị]Phiên bản Python [Được khuyến nghị]. Trăn 3. xDatabase. Không có Loại. Ứng dụng dành cho máy tính để bàn được đề xuất cho lập trình viên Python trung cấp

Python GUI Máy tính khoa học có mã là một dự án đơn giản được tạo bằng Python. Dự án này dựa trên GUI đơn giản và rất đơn giản để hiểu và sử dụng. Ngoài ra, người dùng có thể dễ dàng tìm hiểu cách thực hiện các phép tính số với sự trợ giúp của dự án này. Giống như một máy tính điển hình, dự án có các số, toán tử và ký hiệu. Để nhập một số bất kỳ hoặc chỉ cần nhấp vào các số mà họ muốn để tính toán, người dùng chỉ cần nhấp vào. Do đó, người dùng ứng dụng này có thể sử dụng một máy tính đơn giản

Các tính năng của Máy tính khoa học

  • Tất cả các toán tử cơ bản như cộng, nhân, trừ và chia
  • Các nút chuyên dụng cho sin, cos, tan, cot
  • Các chức năng cho nhật ký
  • Các hàm cho tỷ lệ và số mũ
  • Các nút khác như Pi, mod và x

Mã chính

Bây giờ chúng ta biết những gì chúng ta sẽ phát triển. Hãy để chúng tôi bắt đầu Máy tính khoa học bằng Python bằng hành trình phát triển Tkinter mà không lãng phí một giây nào. Một lời giải thích sâu sắc cùng với mã nguồn chắc chắn sẽ là một kinh nghiệm tốt để tìm hiểu dự án này

Nhập thư viện cơ bản và cấu hình cửa sổ

from tkinter import *
import math
import tkinter.messagebox

root = Tk[]
root.title["Scientific Calculator"]
root.configure[background = 'red']
root.resizable[width=False, height=False]
root.geometry["480x568+450+90"]
calc = Frame[root]
calc.grid[]

Giải trình
Ở đây chúng tôi đã nhập một số thư viện và thiết lập tiêu đề cho Máy tính khoa học của chúng tôi trong cửa sổ Python. Chúng tôi cũng đã sử dụng hàm hình học [] của thư viện Tkinter để đặt kích thước của cửa sổ. Bên cạnh đó, chúng tôi cũng đã sử dụng hàm grid[] để đặt từng ô trong máy tính của nó vào đúng vị trí và thiết kế calc tổng thể dưới dạng lưới

Định nghĩa lớp cho tất cả các hàm Máy tính Khoa học

class Calc[]:
	def __init__[self]:
		self.total=0
		self.current=''
		self.input_value=True
		self.check_sum=False
		self.op=''
		self.result=False

	def numberEnter[self, num]:
		self.result=False
		firstnum=txtDisplay.get[]
		secondnum=str[num]
		if self.input_value:
			self.current = secondnum
			self.input_value=False
		else:
			if secondnum == '.':
				if secondnum in firstnum:
					return
			self.current = firstnum+secondnum
		self.display[self.current]

	def sum_of_total[self]:
		self.result=True
		self.current=float[self.current]
		if self.check_sum==True:
			self.valid_function[]
		else:
			self.total=float[txtDisplay.get[]]

	def display[self, value]:
		txtDisplay.delete[0, END]
		txtDisplay.insert[0, value]

	def valid_function[self]:
		if self.op == "add":
			self.total += self.current
		if self.op == "sub":
			self.total -= self.current
		if self.op == "multi":
			self.total *= self.current
		if self.op == "divide":
			self.total /= self.current
		if self.op == "mod":
			self.total %= self.current
		self.input_value=True
		self.check_sum=False
		self.display[self.total]

	def operation[self, op]:
		self.current = float[self.current]
		if self.check_sum:
			self.valid_function[]
		elif not self.result:
			self.total=self.current
			self.input_value=True
		self.check_sum=True
		self.op=op
		self.result=False

	def Clear_Entry[self]:
		self.result = False
		self.current = "0"
		self.display[0]
		self.input_value=True

	def All_Clear_Entry[self]:
		self.Clear_Entry[]
		self.total=0

	def pi[self]:
		self.result = False
		self.current = math.pi
		self.display[self.current]

	def tau[self]:
		self.result = False
		self.current = math.tau
		self.display[self.current]

	def e[self]:
		self.result = False
		self.current = math.e
		self.display[self.current]

	def mathPM[self]:
		self.result = False
		self.current = -[float[txtDisplay.get[]]]
		self.display[self.current]

	def squared[self]:
		self.result = False
		self.current = math.sqrt[float[txtDisplay.get[]]]
		self.display[self.current]

	def cos[self]:
		self.result = False
		self.current = math.cos[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def cosh[self]:
		self.result = False
		self.current = math.cosh[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def tan[self]:
		self.result = False
		self.current = math.tan[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def tanh[self]:
		self.result = False
		self.current = math.tanh[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def sin[self]:
		self.result = False
		self.current = math.sin[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def sinh[self]:
		self.result = False
		self.current = math.sinh[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def log[self]:
		self.result = False
		self.current = math.log[float[txtDisplay.get[]]]
		self.display[self.current]

	def exp[self]:
		self.result = False
		self.current = math.exp[float[txtDisplay.get[]]]
		self.display[self.current]

	def acosh[self]:
		self.result = False
		self.current = math.acosh[float[txtDisplay.get[]]]
		self.display[self.current]

	def asinh[self]:
		self.result = False
		self.current = math.asinh[float[txtDisplay.get[]]]
		self.display[self.current]

	def expm1[self]:
		self.result = False
		self.current = math.expm1[float[txtDisplay.get[]]]
		self.display[self.current]

	def lgamma[self]:
		self.result = False
		self.current = math.lgamma[float[txtDisplay.get[]]]
		self.display[self.current]

	def degrees[self]:
		self.result = False
		self.current = math.degrees[float[txtDisplay.get[]]]
		self.display[self.current]

	def log2[self]:
		self.result = False
		self.current = math.log2[float[txtDisplay.get[]]]
		self.display[self.current]

	def log10[self]:
		self.result = False
		self.current = math.log10[float[txtDisplay.get[]]]
		self.display[self.current]

	def log1p[self]:
		self.result = False
		self.current = math.log1p[float[txtDisplay.get[]]]
		self.display[self.current]

Giải trình
Trong phần mã này, chúng tôi đã sử dụng lớp để thêm các chức năng khác nhau. Đầu tiên, chúng ta định nghĩa lớp với tên Calc[] và sau đó định nghĩa tất cả các hàm mà chúng ta muốn trong Máy tính Khoa học bằng Python dưới dạng một hàm Python. Ngoài ra, chúng tôi cũng đã xác định hàm để xử lý số đầu vào do người dùng nhập vào. Ngoài ra, để kiểm tra xem hàm có hợp lệ hay không, chúng tôi đã định nghĩa một hàm riêng trong lớp này. Chúng tôi đã bao gồm hầu hết tất cả các chức năng quan trọng có trong một máy tính khoa học bình thường. Trong phần này, chúng tôi đã sử dụng thư viện toán học và các hàm tích hợp khác nhau trong thư viện đó. Các chức năng này được gọi khi nút liên quan đến nó được người dùng nhấp vào. Logic của hàm được tạo bằng các hàm do thư viện toán học cung cấp

Để hiển thị văn bản

added_value = Calc[]

txtDisplay = Entry[calc, font=['Helvetica',20,'bold'],
				bg='black',fg='red',
				bd=30,width=28,justify=RIGHT]
txtDisplay.grid[row=0,column=0, columnspan=4, pady=1]
txtDisplay.insert[0,"0"]

Giải trình
Trong khối mã này, chúng ta đã định nghĩa một biến để lưu trữ lớp Calc[] trong đó. Ngoài ra, để hiển thị hộp văn bản, trong đó người dùng có thể thấy các giá trị và chức năng đã nhập, chúng tôi đã thêm một hộp văn bản và tùy chỉnh nó theo nhu cầu của chúng tôi. Để đặt kích thước của hộp văn bản, chúng tôi đã sử dụng chiều rộng. Ở đây bd là viết tắt của đường viền. Chúng tôi đã thay đổi phông chữ, nền và căn chỉnh văn bản. Chúng tôi cũng đã sử dụng chức năng grid[] của thư viện Tkinter để đặt vị trí của nó theo cách phù hợp

Thêm và sắp xếp các số

numberpad = "789456123"
i=0
btn = []
for j in range[2,5]:
	for k in range[3]:
		btn.append[Button[calc, width=6, height=2,
						bg='black',fg='red',
						font=['Helvetica',20,'bold'],
						bd=4,text=numberpad[i]]]
		btn[i].grid[row=j, column= k, pady = 1]
		btn[i]["command"]=lambda x=numberpad[i]:added_value.numberEnter[x]
		i+=1

Giải trình
Ở đây, trong phần này của Máy tính khoa học trong Python, chúng ta đã định nghĩa một biến có tên là “bàn phím số” lưu trữ tất cả các chữ số. Bây giờ để tạo một hộp riêng cho từng chữ số, chúng tôi đã sử dụng vòng lặp for. Vòng lặp này giúp chúng ta tạo 9 ô cho 9 chữ số sẽ có cùng kích thước và phông chữ. Sử dụng vòng lặp for giúp chúng ta không cần phải viết mã 9 lần để hiển thị các hộp

Các nút xử lý thao tác Clear và Clear all

btnClear = Button[calc, text=chr[67],width=6,
				height=2,bg='powder blue',
				font=['Helvetica',20,'bold']
				,bd=4, command=added_value.Clear_Entry
				].grid[row=1, column= 0, pady = 1]

btnAllClear = Button[calc, text=chr[67]+chr[69],
					width=6, height=2,
					bg='powder blue',
					font=['Helvetica',20,'bold'],
					bd=4,
					command=added_value.All_Clear_Entry
					].grid[row=1, column= 1, pady = 1]

Giải trình
Trong phần mã này cho Máy tính khoa học trong Python, hai nút giúp người dùng xử lý thao tác như xóa một chữ số hoặc một hàm. Các nút Clear All xóa mọi thứ được thêm vào hộp văn bản. Trong phần này, chúng tôi cũng đã tùy chỉnh các nút theo nhu cầu của mình

Các nút cho tất cả các hoạt động của Máy tính tiêu chuẩn

btnsq = Button[calc, text="\u221A",width=6, height=2,
			bg='powder blue', font=['Helvetica',
									20,'bold'],
			bd=4,command=added_value.squared
			].grid[row=1, column= 2, pady = 1]

btnAdd = Button[calc, text="+",width=6, height=2,
				bg='powder blue',
				font=['Helvetica',20,'bold'],
				bd=4,command=lambda:added_value.operation["add"]
				].grid[row=1, column= 3, pady = 1]

btnSub = Button[calc, text="-",width=6,
				height=2,bg='powder blue',
				font=['Helvetica',20,'bold'],
				bd=4,command=lambda:added_value.operation["sub"]
				].grid[row=2, column= 3, pady = 1]

btnMul = Button[calc, text="x",width=6,
				height=2,bg='powder blue',
				font=['Helvetica',20,'bold'],
				bd=4,command=lambda:added_value.operation["multi"]
				].grid[row=3, column= 3, pady = 1]

btnDiv = Button[calc, text="/",width=6,
				height=2,bg='powder blue',
				font=['Helvetica',20,'bold'],
				bd=4,command=lambda:added_value.operation["divide"]
				].grid[row=4, column= 3, pady = 1]

btnZero = Button[calc, text="0",width=6,
				height=2,bg='black',fg='red',
				font=['Helvetica',20,'bold'],
				bd=4,command=lambda:added_value.numberEnter[0]
				].grid[row=5, column= 0, pady = 1]

btnDot = Button[calc, text=".",width=6,
				height=2,bg='powder blue',
				font=['Helvetica',20,'bold'],
				bd=4,command=lambda:added_value.numberEnter["."]
				].grid[row=5, column= 1, pady = 1]
btnPM = Button[calc, text=chr[177],width=6,
			height=2,bg='powder blue', font=['Helvetica',20,'bold'],
			bd=4,command=added_value.mathPM
			].grid[row=5, column= 2, pady = 1]

btnEquals = Button[calc, text="=",width=6,
				height=2,bg='powder blue',
				font=['Helvetica',20,'bold'],
				bd=4,command=added_value.sum_of_total
				].grid[row=5, column= 3, pady = 1]

Giải trình
Các biến trên dành cho các nút thực hiện các thao tác khác nhau như Nhân, Chia, Trừ, Cộng, v.v. Các nút này khi được nhấp vào sẽ gọi chức năng tương ứng được gắn với chúng, do đó giúp người dùng thực hiện thao tác mong muốn một cách dễ dàng. Với mục đích thiết kế, chúng tôi đã sử dụng tất cả các thuộc tính được cung cấp bởi chức năng Nút của thư viện Tkinter. Trên hết, hàm grid[] cũng được sử dụng ở đâu đó

Các nút cho Máy tính khoa học – Hàng 1

btnPi = Button[calc, text="pi",width=6,
			height=2,bg='black',fg='red',
			font=['Helvetica',20,'bold'],
			bd=4,command=added_value.pi
			].grid[row=1, column= 4, pady = 1]

btnCos = Button[calc, text="Cos",width=6,
				height=2,bg='black',fg='red',
				font=['Helvetica',20,'bold'],
				bd=4,command=added_value.cos
			].grid[row=1, column= 5, pady = 1]

btntan = Button[calc, text="tan",width=6,
				height=2,bg='black',fg='red',
				font=['Helvetica',20,'bold'],
				bd=4,command=added_value.tan
			].grid[row=1, column= 6, pady = 1]

btnsin = Button[calc, text="sin",width=6,
				height=2,bg='black',fg='red',
				font=['Helvetica',20,'bold'],
				bd=4,command=added_value.sin
			].grid[row=1, column= 7, pady = 1]

Giải trình
Trong hàng này, chúng tôi đã thêm các nút cho các chức năng của Pi, sin, cos và tan. Mô hình tương tự này diễn ra cho 4 hàng khác. Ngoài ra, chúng tôi đã thêm các chức năng cùng với các nút tương ứng

Các nút cho Máy tính khoa học – Hàng 2

btn2Pi = Button[calc, text="2pi",width=6,
				height=2,bg='black',fg='red',
				font=['Helvetica',20,'bold'],
				bd=4,command=added_value.tau
			].grid[row=2, column= 4, pady = 1]

btnCosh = Button[calc, text="Cosh",width=6,
				height=2,bg='black',fg='red',
				font=['Helvetica',20,'bold'],
				bd=4,command=added_value.cosh
				].grid[row=2, column= 5, pady = 1]

btntanh = Button[calc, text="tanh",width=6,
				height=2,bg='black',fg='red',
				font=['Helvetica',20,'bold'],
				bd=4,command=added_value.tanh
				].grid[row=2, column= 6, pady = 1]

btnsinh = Button[calc, text="sinh",width=6,
				height=2,bg='black',fg='red',
				font=['Helvetica',20,'bold'],
				bd=4,command=added_value.sinh
				].grid[row=2, column= 7, pady = 1]

Các nút cho Máy tính khoa học – Hàng 3

btnlog = Button[calc, text="log",width=6,
				height=2,bg='black',fg='red',
				font=['Helvetica',20,'bold'],
				bd=4,command=added_value.log
			].grid[row=3, column= 4, pady = 1]

btnExp = Button[calc, text="exp",width=6, height=2,
				bg='black',fg='red',
				font=['Helvetica',20,'bold'],
				bd=4,command=added_value.exp
			].grid[row=3, column= 5, pady = 1]

btnMod = Button[calc, text="Mod",width=6,
				height=2,bg='black',fg='red',
				font=['Helvetica',20,'bold'],
				bd=4,command=lambda:added_value.operation["mod"]
				].grid[row=3, column= 6, pady = 1]

btnE = Button[calc, text="e",width=6,
				height=2,bg='black',fg='red',
				font=['Helvetica',20,'bold'],
				bd=4,command=added_value.e
			].grid[row=3, column= 7, pady = 1]

Các nút cho Máy tính khoa học – Hàng 4

btnlog10 = Button[calc, text="log10",width=6,
				height=2,bg='black',fg='red',
				font=['Helvetica',20,'bold'],
				bd=4,command=added_value.log10
				].grid[row=4, column= 4, pady = 1]

btncos = Button[calc, text="log1p",width=6,
				height=2,bg='black',fg='red',
				font=['Helvetica',20,'bold'],
				bd=4,command=added_value.log1p
				].grid[row=4, column= 5, pady = 1]

btnexpm1 = Button[calc, text="expm1",width=6,
				height=2,bg='black',fg='red',
				font=['Helvetica',20,'bold'],
				bd = 4,command=added_value.expm1
				].grid[row=4, column= 6, pady = 1]

btngamma = Button[calc, text="gamma",width=6,
				height=2,bg='black',fg='red',
				font=['Helvetica',20,'bold'],
				bd=4,command=added_value.lgamma
				].grid[row=4, column= 7, pady = 1]

Các nút cho Máy tính khoa học – Hàng 5

class Calc[]:
	def __init__[self]:
		self.total=0
		self.current=''
		self.input_value=True
		self.check_sum=False
		self.op=''
		self.result=False

	def numberEnter[self, num]:
		self.result=False
		firstnum=txtDisplay.get[]
		secondnum=str[num]
		if self.input_value:
			self.current = secondnum
			self.input_value=False
		else:
			if secondnum == '.':
				if secondnum in firstnum:
					return
			self.current = firstnum+secondnum
		self.display[self.current]

	def sum_of_total[self]:
		self.result=True
		self.current=float[self.current]
		if self.check_sum==True:
			self.valid_function[]
		else:
			self.total=float[txtDisplay.get[]]

	def display[self, value]:
		txtDisplay.delete[0, END]
		txtDisplay.insert[0, value]

	def valid_function[self]:
		if self.op == "add":
			self.total += self.current
		if self.op == "sub":
			self.total -= self.current
		if self.op == "multi":
			self.total *= self.current
		if self.op == "divide":
			self.total /= self.current
		if self.op == "mod":
			self.total %= self.current
		self.input_value=True
		self.check_sum=False
		self.display[self.total]

	def operation[self, op]:
		self.current = float[self.current]
		if self.check_sum:
			self.valid_function[]
		elif not self.result:
			self.total=self.current
			self.input_value=True
		self.check_sum=True
		self.op=op
		self.result=False

	def Clear_Entry[self]:
		self.result = False
		self.current = "0"
		self.display[0]
		self.input_value=True

	def All_Clear_Entry[self]:
		self.Clear_Entry[]
		self.total=0

	def pi[self]:
		self.result = False
		self.current = math.pi
		self.display[self.current]

	def tau[self]:
		self.result = False
		self.current = math.tau
		self.display[self.current]

	def e[self]:
		self.result = False
		self.current = math.e
		self.display[self.current]

	def mathPM[self]:
		self.result = False
		self.current = -[float[txtDisplay.get[]]]
		self.display[self.current]

	def squared[self]:
		self.result = False
		self.current = math.sqrt[float[txtDisplay.get[]]]
		self.display[self.current]

	def cos[self]:
		self.result = False
		self.current = math.cos[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def cosh[self]:
		self.result = False
		self.current = math.cosh[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def tan[self]:
		self.result = False
		self.current = math.tan[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def tanh[self]:
		self.result = False
		self.current = math.tanh[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def sin[self]:
		self.result = False
		self.current = math.sin[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def sinh[self]:
		self.result = False
		self.current = math.sinh[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def log[self]:
		self.result = False
		self.current = math.log[float[txtDisplay.get[]]]
		self.display[self.current]

	def exp[self]:
		self.result = False
		self.current = math.exp[float[txtDisplay.get[]]]
		self.display[self.current]

	def acosh[self]:
		self.result = False
		self.current = math.acosh[float[txtDisplay.get[]]]
		self.display[self.current]

	def asinh[self]:
		self.result = False
		self.current = math.asinh[float[txtDisplay.get[]]]
		self.display[self.current]

	def expm1[self]:
		self.result = False
		self.current = math.expm1[float[txtDisplay.get[]]]
		self.display[self.current]

	def lgamma[self]:
		self.result = False
		self.current = math.lgamma[float[txtDisplay.get[]]]
		self.display[self.current]

	def degrees[self]:
		self.result = False
		self.current = math.degrees[float[txtDisplay.get[]]]
		self.display[self.current]

	def log2[self]:
		self.result = False
		self.current = math.log2[float[txtDisplay.get[]]]
		self.display[self.current]

	def log10[self]:
		self.result = False
		self.current = math.log10[float[txtDisplay.get[]]]
		self.display[self.current]

	def log1p[self]:
		self.result = False
		self.current = math.log1p[float[txtDisplay.get[]]]
		self.display[self.current]
0

Tùy chọn thanh menu

class Calc[]:
	def __init__[self]:
		self.total=0
		self.current=''
		self.input_value=True
		self.check_sum=False
		self.op=''
		self.result=False

	def numberEnter[self, num]:
		self.result=False
		firstnum=txtDisplay.get[]
		secondnum=str[num]
		if self.input_value:
			self.current = secondnum
			self.input_value=False
		else:
			if secondnum == '.':
				if secondnum in firstnum:
					return
			self.current = firstnum+secondnum
		self.display[self.current]

	def sum_of_total[self]:
		self.result=True
		self.current=float[self.current]
		if self.check_sum==True:
			self.valid_function[]
		else:
			self.total=float[txtDisplay.get[]]

	def display[self, value]:
		txtDisplay.delete[0, END]
		txtDisplay.insert[0, value]

	def valid_function[self]:
		if self.op == "add":
			self.total += self.current
		if self.op == "sub":
			self.total -= self.current
		if self.op == "multi":
			self.total *= self.current
		if self.op == "divide":
			self.total /= self.current
		if self.op == "mod":
			self.total %= self.current
		self.input_value=True
		self.check_sum=False
		self.display[self.total]

	def operation[self, op]:
		self.current = float[self.current]
		if self.check_sum:
			self.valid_function[]
		elif not self.result:
			self.total=self.current
			self.input_value=True
		self.check_sum=True
		self.op=op
		self.result=False

	def Clear_Entry[self]:
		self.result = False
		self.current = "0"
		self.display[0]
		self.input_value=True

	def All_Clear_Entry[self]:
		self.Clear_Entry[]
		self.total=0

	def pi[self]:
		self.result = False
		self.current = math.pi
		self.display[self.current]

	def tau[self]:
		self.result = False
		self.current = math.tau
		self.display[self.current]

	def e[self]:
		self.result = False
		self.current = math.e
		self.display[self.current]

	def mathPM[self]:
		self.result = False
		self.current = -[float[txtDisplay.get[]]]
		self.display[self.current]

	def squared[self]:
		self.result = False
		self.current = math.sqrt[float[txtDisplay.get[]]]
		self.display[self.current]

	def cos[self]:
		self.result = False
		self.current = math.cos[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def cosh[self]:
		self.result = False
		self.current = math.cosh[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def tan[self]:
		self.result = False
		self.current = math.tan[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def tanh[self]:
		self.result = False
		self.current = math.tanh[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def sin[self]:
		self.result = False
		self.current = math.sin[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def sinh[self]:
		self.result = False
		self.current = math.sinh[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def log[self]:
		self.result = False
		self.current = math.log[float[txtDisplay.get[]]]
		self.display[self.current]

	def exp[self]:
		self.result = False
		self.current = math.exp[float[txtDisplay.get[]]]
		self.display[self.current]

	def acosh[self]:
		self.result = False
		self.current = math.acosh[float[txtDisplay.get[]]]
		self.display[self.current]

	def asinh[self]:
		self.result = False
		self.current = math.asinh[float[txtDisplay.get[]]]
		self.display[self.current]

	def expm1[self]:
		self.result = False
		self.current = math.expm1[float[txtDisplay.get[]]]
		self.display[self.current]

	def lgamma[self]:
		self.result = False
		self.current = math.lgamma[float[txtDisplay.get[]]]
		self.display[self.current]

	def degrees[self]:
		self.result = False
		self.current = math.degrees[float[txtDisplay.get[]]]
		self.display[self.current]

	def log2[self]:
		self.result = False
		self.current = math.log2[float[txtDisplay.get[]]]
		self.display[self.current]

	def log10[self]:
		self.result = False
		self.current = math.log10[float[txtDisplay.get[]]]
		self.display[self.current]

	def log1p[self]:
		self.result = False
		self.current = math.log1p[float[txtDisplay.get[]]]
		self.display[self.current]
1

Giải trình
Trong phần này chúng ta đã code để tạo nên Menubar. Điều này sẽ đơn giản xuất hiện trên đầu trang của máy tính. Chúng tôi đã thêm nhiều tùy chọn như Tệp và Chỉnh sửa. Ngoài ra, để xử lý việc mở và đóng cửa sổ Khoa học calc, chúng tôi đã xác định hai hàm mới là Standard[] và Scientific[]. Bên trong các chức năng này, chúng tôi đã sử dụng các chức năng hình học và thay đổi kích thước của thư viện Tkinter

Tùy chọn phụ trong Menu Bar

class Calc[]:
	def __init__[self]:
		self.total=0
		self.current=''
		self.input_value=True
		self.check_sum=False
		self.op=''
		self.result=False

	def numberEnter[self, num]:
		self.result=False
		firstnum=txtDisplay.get[]
		secondnum=str[num]
		if self.input_value:
			self.current = secondnum
			self.input_value=False
		else:
			if secondnum == '.':
				if secondnum in firstnum:
					return
			self.current = firstnum+secondnum
		self.display[self.current]

	def sum_of_total[self]:
		self.result=True
		self.current=float[self.current]
		if self.check_sum==True:
			self.valid_function[]
		else:
			self.total=float[txtDisplay.get[]]

	def display[self, value]:
		txtDisplay.delete[0, END]
		txtDisplay.insert[0, value]

	def valid_function[self]:
		if self.op == "add":
			self.total += self.current
		if self.op == "sub":
			self.total -= self.current
		if self.op == "multi":
			self.total *= self.current
		if self.op == "divide":
			self.total /= self.current
		if self.op == "mod":
			self.total %= self.current
		self.input_value=True
		self.check_sum=False
		self.display[self.total]

	def operation[self, op]:
		self.current = float[self.current]
		if self.check_sum:
			self.valid_function[]
		elif not self.result:
			self.total=self.current
			self.input_value=True
		self.check_sum=True
		self.op=op
		self.result=False

	def Clear_Entry[self]:
		self.result = False
		self.current = "0"
		self.display[0]
		self.input_value=True

	def All_Clear_Entry[self]:
		self.Clear_Entry[]
		self.total=0

	def pi[self]:
		self.result = False
		self.current = math.pi
		self.display[self.current]

	def tau[self]:
		self.result = False
		self.current = math.tau
		self.display[self.current]

	def e[self]:
		self.result = False
		self.current = math.e
		self.display[self.current]

	def mathPM[self]:
		self.result = False
		self.current = -[float[txtDisplay.get[]]]
		self.display[self.current]

	def squared[self]:
		self.result = False
		self.current = math.sqrt[float[txtDisplay.get[]]]
		self.display[self.current]

	def cos[self]:
		self.result = False
		self.current = math.cos[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def cosh[self]:
		self.result = False
		self.current = math.cosh[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def tan[self]:
		self.result = False
		self.current = math.tan[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def tanh[self]:
		self.result = False
		self.current = math.tanh[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def sin[self]:
		self.result = False
		self.current = math.sin[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def sinh[self]:
		self.result = False
		self.current = math.sinh[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def log[self]:
		self.result = False
		self.current = math.log[float[txtDisplay.get[]]]
		self.display[self.current]

	def exp[self]:
		self.result = False
		self.current = math.exp[float[txtDisplay.get[]]]
		self.display[self.current]

	def acosh[self]:
		self.result = False
		self.current = math.acosh[float[txtDisplay.get[]]]
		self.display[self.current]

	def asinh[self]:
		self.result = False
		self.current = math.asinh[float[txtDisplay.get[]]]
		self.display[self.current]

	def expm1[self]:
		self.result = False
		self.current = math.expm1[float[txtDisplay.get[]]]
		self.display[self.current]

	def lgamma[self]:
		self.result = False
		self.current = math.lgamma[float[txtDisplay.get[]]]
		self.display[self.current]

	def degrees[self]:
		self.result = False
		self.current = math.degrees[float[txtDisplay.get[]]]
		self.display[self.current]

	def log2[self]:
		self.result = False
		self.current = math.log2[float[txtDisplay.get[]]]
		self.display[self.current]

	def log10[self]:
		self.result = False
		self.current = math.log10[float[txtDisplay.get[]]]
		self.display[self.current]

	def log1p[self]:
		self.result = False
		self.current = math.log1p[float[txtDisplay.get[]]]
		self.display[self.current]
2

Giải trình
Nói về các tùy chọn phụ trong nút “Tệp”, có một tính năng để chuyển đổi giữa Máy tính “Chuẩn” và “Khoa học”. Bên trong nút “Chỉnh sửa” có tùy chọn Sao chép, Dán và Cắt. Các tùy chọn này sẽ giúp sao chép câu trả lời, dán các giá trị khác vào calc này và cũng có thể cắt câu trả lời. Bất cứ khi nào người dùng nhấp vào Máy tính khoa học, một cửa sổ bên cạnh Máy tính tiêu chuẩn sẽ mở ra. Một cửa sổ riêng biệt giúp người dùng đóng và truy cập Khoa học calc với nhiều mục đích sử dụng hơn và do đó chúng tôi quyết định viết mã theo cách như vậy

Đầu ra cho Máy tính khoa học GUI bằng Python

Để chuyển từ Máy tính tiêu chuẩn sang Máy tính khoa học

Mã hoàn chỉnh cho Máy tính khoa học GUI bằng Python trong Tkinter

class Calc[]:
	def __init__[self]:
		self.total=0
		self.current=''
		self.input_value=True
		self.check_sum=False
		self.op=''
		self.result=False

	def numberEnter[self, num]:
		self.result=False
		firstnum=txtDisplay.get[]
		secondnum=str[num]
		if self.input_value:
			self.current = secondnum
			self.input_value=False
		else:
			if secondnum == '.':
				if secondnum in firstnum:
					return
			self.current = firstnum+secondnum
		self.display[self.current]

	def sum_of_total[self]:
		self.result=True
		self.current=float[self.current]
		if self.check_sum==True:
			self.valid_function[]
		else:
			self.total=float[txtDisplay.get[]]

	def display[self, value]:
		txtDisplay.delete[0, END]
		txtDisplay.insert[0, value]

	def valid_function[self]:
		if self.op == "add":
			self.total += self.current
		if self.op == "sub":
			self.total -= self.current
		if self.op == "multi":
			self.total *= self.current
		if self.op == "divide":
			self.total /= self.current
		if self.op == "mod":
			self.total %= self.current
		self.input_value=True
		self.check_sum=False
		self.display[self.total]

	def operation[self, op]:
		self.current = float[self.current]
		if self.check_sum:
			self.valid_function[]
		elif not self.result:
			self.total=self.current
			self.input_value=True
		self.check_sum=True
		self.op=op
		self.result=False

	def Clear_Entry[self]:
		self.result = False
		self.current = "0"
		self.display[0]
		self.input_value=True

	def All_Clear_Entry[self]:
		self.Clear_Entry[]
		self.total=0

	def pi[self]:
		self.result = False
		self.current = math.pi
		self.display[self.current]

	def tau[self]:
		self.result = False
		self.current = math.tau
		self.display[self.current]

	def e[self]:
		self.result = False
		self.current = math.e
		self.display[self.current]

	def mathPM[self]:
		self.result = False
		self.current = -[float[txtDisplay.get[]]]
		self.display[self.current]

	def squared[self]:
		self.result = False
		self.current = math.sqrt[float[txtDisplay.get[]]]
		self.display[self.current]

	def cos[self]:
		self.result = False
		self.current = math.cos[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def cosh[self]:
		self.result = False
		self.current = math.cosh[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def tan[self]:
		self.result = False
		self.current = math.tan[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def tanh[self]:
		self.result = False
		self.current = math.tanh[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def sin[self]:
		self.result = False
		self.current = math.sin[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def sinh[self]:
		self.result = False
		self.current = math.sinh[math.radians[float[txtDisplay.get[]]]]
		self.display[self.current]

	def log[self]:
		self.result = False
		self.current = math.log[float[txtDisplay.get[]]]
		self.display[self.current]

	def exp[self]:
		self.result = False
		self.current = math.exp[float[txtDisplay.get[]]]
		self.display[self.current]

	def acosh[self]:
		self.result = False
		self.current = math.acosh[float[txtDisplay.get[]]]
		self.display[self.current]

	def asinh[self]:
		self.result = False
		self.current = math.asinh[float[txtDisplay.get[]]]
		self.display[self.current]

	def expm1[self]:
		self.result = False
		self.current = math.expm1[float[txtDisplay.get[]]]
		self.display[self.current]

	def lgamma[self]:
		self.result = False
		self.current = math.lgamma[float[txtDisplay.get[]]]
		self.display[self.current]

	def degrees[self]:
		self.result = False
		self.current = math.degrees[float[txtDisplay.get[]]]
		self.display[self.current]

	def log2[self]:
		self.result = False
		self.current = math.log2[float[txtDisplay.get[]]]
		self.display[self.current]

	def log10[self]:
		self.result = False
		self.current = math.log10[float[txtDisplay.get[]]]
		self.display[self.current]

	def log1p[self]:
		self.result = False
		self.current = math.log1p[float[txtDisplay.get[]]]
		self.display[self.current]
3

Liên kết hướng dẫn tham khảo YouTube

Liên kết thư viện chính thức

  • thư viện toán học. Liên kết tài liệu chính thức
  • thư viện tkinter. Liên kết tài liệu chính thức

chú thích cuối

Cuối cùng. Đây là phần cuối của bài viết của chúng tôi về Máy tính khoa học trong Python bằng Tkinter. Chúng tôi thực sự hy vọng rằng bài viết này hóa ra là một nguồn trợ giúp tuyệt vời cho bạn. Chúng tôi đã cố gắng hết sức để giải thích từng khái niệm liên quan đến hướng dẫn này. Tại CopyAssignments, chúng tôi tin chắc rằng việc phát triển các dự án là cách duy nhất để trở thành người tiên phong của Python. Trong dự án Máy tính khoa học GUI trong Python này, chúng tôi đã thêm một số chức năng cơ bản đến trung gian, bây giờ đến lượt bạn thêm các chức năng nâng cao và nâng cấp dự án lên cấp độ tiếp theo. Chúng tôi sẽ sớm quay lại với một hướng dẫn khác, cho đến lúc đó hãy tiếp tục Học, Thực thi và Xây dựng. Cảm ơn bạn đã ghé thăm website của chúng tôi

Tôi có thể tạo máy tính bằng Python không?

Lập trình Python là một cách thú vị để học cách viết mã cho trẻ em từ 8-18 tuổi. Bạn có thể tạo một máy tính cơ bản để thực hiện các phép toán số học, bao gồm cộng, trừ, nhân và chia .

Làm cách nào để tạo máy tính GUI bằng Python?

Python cung cấp nhiều tùy chọn để phát triển GUI [Giao diện người dùng đồ họa]. Trong số tất cả các phương pháp GUI, Tkinter là phương pháp được sử dụng phổ biến nhất. .
Nhập mô-đun – tkinter
Tạo cửa sổ chính [vùng chứa]
Thêm bất kỳ số lượng vật dụng nào vào cửa sổ chính
Áp dụng Trình kích hoạt sự kiện trên các vật dụng

Làm cách nào để tạo một máy tính khoa học bằng Python bằng Tkinter?

Điều kiện tiên quyết. GUI Python – tkinter
Bước 1. Nhập mô-đun
Bước 2. Ở đây chúng ta sẽ tạo hình học hay cái gọi là bố cục cho GUI của máy tính bằng cách sử dụng Tkinter
Bước 3. Bây giờ chúng ta sẽ tạo một lớp trong đó chúng ta sẽ tạo tất cả các chức năng của máy tính khoa học để chúng có thể được gọi và thực hiện dễ dàng

Làm cách nào để tạo một ứng dụng máy tính bằng Python?

Cách tạo Máy tính trong Python Tkinter .
Tại sao sử dụng Tkinter để xây dựng GUI Python?
1] Nhập Tkinter
2] Tạo Bộ chứa Tkinter
3] Xác định chức năng Tkinter Widget
4] Thiết kế ứng dụng
5] Chạy ứng dụng

Chủ Đề