Programmacode bij De simpele rekenmachine
#Rekenmachine v.0.1 import math import numpy import tkinter as tk from tkinter import filedialog import tkinter as tk def Eval(R): SYM = ["+", "-", "x", ":"] for sym in SYM: if sym in R: S = R.split(sym) if len(S[1]) == 0: return S[0] if sym == "+": n = float(S[0]) + float(S[1]) if sym == "-": n = float(S[0]) - float(S[1]) if sym == "x": n = float(S[0]) * float(S[1]) if sym == ":": if float(S[1]) == 0: n = 0 else: n = float(S[0]) / float(S[1]) return str(n) return R def Click(n): global R if n == 0: # wortel R = Eval(R) t = float(R) if t < 0: m = 0 else: m = math.sqrt(t) R = str(m) if n == 1: # kwadraat R = Eval(R) t = float(R) m = t*t R = str(m) if n == 2: # delen R = Eval(R)+":" if n == 3: # vermenigvuldigen R = Eval(R)+"x" if n == 4: # 7 if R == "0": R = "" R += "7" if n == 5: # 8 if R == "0": R = "" R += "8" if n == 6: # 9 if R == "0": R = "" R += "9" if n == 7: # aftrekken R = Eval(R)+"-" if n == 8: # 4 if R == "0": R = "" R += "4" if n == 9: # 5 if R == "0": R = "" R += "5" if n == 10: # 6 if R == "0": R = "" R += "6" if n == 11: # optellen R = Eval(R)+"+" if n == 12: # 1 if R == "0": R = "" R += "1" if n == 13: # 2 if R == "0": R = "" R += "2" if n == 14: # 3 if R == "0": R = "" R += "3" if n == 15: # Enter (Resultaat bekijken) R = Eval(R) if n == 16: # Clear R = "0" if n == 17: # 0 if R == "0": R = "" R += "0" if n == 18: # Del lr = len(R) R = R[0:lr-1] if R == "": R = "0" if n == 19: # exit DestroyButtons() return text.delete('1.0', tk.END) text.insert(tk.INSERT, R+"\n") return def DestroyButtons(): for y in range(5): for x in range (4): buttons[y][x].destroy() text.destroy() root.destroy() return def ShowButtons(): text.grid(row=0, column=0, columnspan = 4, sticky="nsew") #root.grid_rowconfigure(1, weight=1) #root.grid_columnconfigure(1, weight=1) for yy in range (5): y = yy+1 button_row = [] for x in range (4): color= "light gray" nr = 4*yy+x button = tk.Button(root, text=TEKSTEN[nr], width=5, height=1, bg=color, fg="black", borderwidth = 1, font=(FontName, FontSize, 'bold'), command=lambda nr = nr: Click(nr)) button.grid(row=y, column=x, sticky="nsew") button_row.append(button) buttons.append(button_row) root.grid_rowconfigure(6, weight=1) root.grid_columnconfigure(4, weight=1) return FontName = 'Verdana' FontSize = '18' root = tk.Tk() text = tk.Text(root, height = 1, width = 18, font = (FontName, FontSize, 'bold')) TEKSTEN = [" wortel "," kwad ","÷","×", "7","8","9","-", "4","5","6","+", "1","2","3","=", "Clear","0","Del","eXit"] R = "0" buttons = [] ShowButtons() root.mainloop()
#Terug naar boven import math import numpy import tkinter as tk import random import sys root = tk.Tk() button = tk.Button(root, text="Exit", width=4, height=1, bg="light gray", fg="black", borderwidth = 1, font=('Helvetica', '32', 'bold'), command=lambda root=root: root.destroy()) button.grid(row=0, column=0, sticky="nsew") root.grid_rowconfigure(1, weight=1) root.grid_columnconfigure(1, weight=1) root.mainloop()
Programmacode bij opdracht 2
#Rekenmachine v.0.1 import math import numpy import tkinter as tk from tkinter import filedialog import tkinter as tk def Eval(R): SYM = ["+", "-", "x", ":"] for sym in SYM: if sym in R: S = R.split(sym) if len(S[1]) == 0: return S[0] if sym == "+": n = float(S[0]) + float(S[1]) if sym == "-": n = float(S[0]) - float(S[1]) if sym == "x": n = float(S[0]) * float(S[1]) if sym == ":": if float(S[1]) == 0: n = 0 else: n = float(S[0]) / float(S[1]) return str(n) return R def Click(nn): global R n = nn-4 if n == -4: # log R = Eval(R) if n == -3: # sin R = Eval(R) if n == -2: # cos R = Eval(R) if n == -1: # tan R = Eval(R) if n == 0: # wortel R = Eval(R) t = float(R) if t < 0: m = 0 else: m = math.sqrt(t) R = str(m) if n == 1: # kwadraat R = Eval(R) t = float(R) m = t*t R = str(m) if n == 2: # delen R = Eval(R)+":" if n == 3: # vermenigvuldigen R = Eval(R)+"x" if n == 4: # 7 if R == "0": R = "" R += "7" if n == 5: # 8 if R == "0": R = "" R += "8" if n == 6: # 9 if R == "0": R = "" R += "9" if n == 7: # aftrekken R = Eval(R)+"-" if n == 8: # 4 if R == "0": R = "" R += "4" if n == 9: # 5 if R == "0": R = "" R += "5" if n == 10: # 6 if R == "0": R = "" R += "6" if n == 11: # optellen R = Eval(R)+"+" if n == 12: # 1 if R == "0": R = "" R += "1" if n == 13: # 2 if R == "0": R = "" R += "2" if n == 14: # 3 if R == "0": R = "" R += "3" if n == 15: # Enter (Resultaat bekijken) R = Eval(R) if n == 16: # Clear R = "0" if n == 17: # 0 if R == "0": R = "" R += "0" if n == 18: # Del lr = len(R) R = R[0:lr-1] if R == "": R = "0" if n == 19: # exit DestroyButtons() return text.delete('1.0', tk.END) text.insert(tk.INSERT, R+"\n") return def DestroyButtons(): for y in range(5): for x in range (4): buttons[y][x].destroy() text.destroy() root.destroy() return def ShowButtons(): text.grid(row=0, column=0, columnspan = 4, sticky="nsew") #root.grid_rowconfigure(1, weight=1) #root.grid_columnconfigure(1, weight=1) for yy in range (6): y = yy+1 button_row = [] for x in range (4): color= "light gray" nr = 4*yy+x button = tk.Button(root, text=TEKSTEN[nr], width=4, height=1, bg=color, fg="black", borderwidth = 1, font=(FontName, FontSize, 'bold'), command=lambda nr = nr: Click(nr)) button.grid(row=y, column=x, sticky="nsew") button_row.append(button) buttons.append(button_row) root.grid_rowconfigure(7, weight=1) root.grid_columnconfigure(4, weight=1) return FontName = 'Verdana' FontSize = '14' root = tk.Tk() text = tk.Text(root, height = 1, width = 16, font = (FontName, FontSize, 'bold')) TEKSTEN = ["exp","sin","cos","tan", "inv","kwad",":","x", "7","8","9","-", "4","5","6","+", "1","2","3","=", "Clear","0","Del","eXit"] R = "0" buttons = [] ShowButtons() root.mainloop()
Programmacode bij Opdracht 4
#Rekenmachine v.0.1 import math import numpy import tkinter as tk from tkinter import filedialog import tkinter as tk def Eval(R): SYM = ["+", "-", "x", ":"] for sym in SYM: if sym in R: S = R.split(sym) if len(S[1]) == 0: return S[0] if sym == "+": n = float(S[0]) + float(S[1]) if sym == "-": n = float(S[0]) - float(S[1]) if sym == "x": n = float(S[0]) * float(S[1]) if sym == ":": if float(S[1]) == 0: n = 0 else: n = float(S[0]) / float(S[1]) return str(n) return R def Click(nn): global R n = nn-4 if n == -4: # log R = Eval(R) if n == -3: # sin R = Eval(R) if n == -2: # cos R = Eval(R) if n == -1: # tan R = Eval(R) if n == 0: # wortel R = Eval(R) t = float(R) if t < 0: m = 0 else: m = math.sqrt(t) R = str(m) if n == 1: # kwadraat R = Eval(R) t = float(R) m = t*t R = str(m) if n == 2: # delen R = Eval(R)+":" if n == 3: # vermenigvuldigen R = Eval(R)+"x" if n == 4: # 7 if R == "0": R = "" R += "7" if n == 5: # 8 if R == "0": R = "" R += "8" if n == 6: # 9 if R == "0": R = "" R += "9" if n == 7: # aftrekken R = Eval(R)+"-" if n == 8: # 4 if R == "0": R = "" R += "4" if n == 9: # 5 if R == "0": R = "" R += "5" if n == 10: # 6 if R == "0": R = "" R += "6" if n == 11: # optellen R = Eval(R)+"+" if n == 12: # 1 if R == "0": R = "" R += "1" if n == 13: # 2 if R == "0": R = "" R += "2" if n == 14: # 3 if R == "0": R = "" R += "3" if n == 15: # Enter (Resultaat bekijken) R = Eval(R) if n == 16: # Clear R = "0" if n == 17: # 0 if R == "0": R = "" R += "0" if n == 18: # Del lr = len(R) R = R[0:lr-1] if R == "": R = "0" if n == 19: # exit DestroyButtons() return text.delete('1.0', tk.END) text.insert(tk.INSERT, R+"\n") return def DestroyButtons(): for y in range(5): for x in range (4): buttons[y][x].destroy() text.destroy() root.destroy() return def ShowButtons(): text.grid(row=0, column=0, columnspan = 4, sticky="nsew") #root.grid_rowconfigure(1, weight=1) #root.grid_columnconfigure(1, weight=1) for yy in range (6): y = yy+1 button_row = [] for x in range (4): color= "light gray" nr = 4*yy+x button = tk.Button(root, text=TEKSTEN[nr], width=4, height=1, bg=color, fg="black", borderwidth = 1, font=(FontName, FontSize, 'bold'), command=lambda nr = nr: Click(nr)) button.grid(row=y, column=x, sticky="nsew") button_row.append(button) buttons.append(button_row) root.grid_rowconfigure(7, weight=1) root.grid_columnconfigure(4, weight=1) return FontName = 'Verdana' FontSize = '14' root = tk.Tk() text = tk.Text(root, height = 1, width = 16, font = (FontName, FontSize, 'bold')) TEKSTEN = ["exp","sin","cos","tan", "inv","kwad",":","x", "7","8","9","-", "4","5","6","+", "1","2","3","=", "Clear","0","Del","eXit"] R = "0" buttons = [] ShowButtons() root.mainloop()
Programmacode bij Opdracht 5
#Rekenmachine v.0.1 import math import tkinter as tk def Eval(R): sgn = "" if len(R) > 0 and R[0] == "-": # Haal min teken van eerste getal af R = R[1:] sgn = "-" if len(R) > 0 and not R[0].isdigit(): #R[0] moet een digit zijn R = "Error" return R SYM = ["+", "-", "x", ":"] for sym in SYM: if sym in R: # Controleer operatie S = R.split(sym) s0 = float(sgn + S[0]) if len(S[1]) == 0: result = str(s0) else: s1 = float(S[1]) if sym == "+": result = str(s0 + s1) if sym == "-": result = str(s0 - s1) if sym == "x": result = str(s0 * s1) if sym == ":": if s1 == 0: result = "Error" else: result = str(s0/s1) return result return sgn+R def Click(nr): global R nr = nr-4 ev = Eval(R) if "Error" in ev: if nr in [16,18]: R = "" ev = "" text.configure(state = "normal") # Er kan alleen met de toetsen worden gewerkt text.delete("1.0", tk.END) text.insert(tk.INSERT,ev) text.configure(state = "disabled") return if nr == -4: # log R = Eval(R) if nr == -3: # sin R = Eval(R) if nr == -2: # cos R = Eval(R) if nr == -1: # tan R = Eval(R) lr = len(R) if nr == 0 and lr > 0: # wortel R = ev n = float(R) if n < 0: R = "Error" return else: m = math.sqrt(n) R = str(m) if nr == 1 and lr > 0 : # kwadraat R = ev n = float(R) m = n*n R = str(m) if nr == 2: # Delen R = ev + ":" if nr == 3: # Vermenigvuldigen R = ev + "x" if nr in range(4,7): # 7, 8 en 9 R += str(nr + 3) if nr == 7: # Aftrekken R = ev + "-" if nr in range(8,11): # 4, 5 en 6 R += str(nr - 4) if nr == 11: # Optellen R = ev + "+" if nr in range(12,15): # 1, 2 en 3 R += str(nr - 11) if nr == 15: # Enter (Resultaat bekijken) R = ev if nr == 16: # Clear R = "" if nr == 17: # 0 R += str(0) if nr == 18: # Del lr = len(R) if lr > 0: R = R[0:lr-1] if nr == 19: # exit DestroyButtons() return text.configure(state = "normal") # Er kan alleen met de toetsen worden gewerkt text.delete("1.0", tk.END) text.insert(tk.INSERT, R) text.configure(state = "disabled") return def DestroyButtons(): for y in range(5): for x in range (4): buttons[y][x].destroy() text.destroy() root.destroy() return def ShowButtons(): text.grid(row=0, column=0, columnspan = 3, sticky="nsew") button = tk.Button(root, text="eXit", width=3, height=1, bg=color, fg="black", borderwidth = 1, font=(FontName, FontSize, "bold"), command=lambda root=root: DestroyButtons()) button.grid(row=0, column=3, sticky="nsew") #root.grid_rowconfigure(1, weight=1) #root.grid_columnconfigure(1, weight=1) for yy in range (6): y = yy+1 button_row = [] for x in range (4): nr = 4*yy+x button = tk.Button(root, text=TEKSTEN[nr], width=3, height=1, bg=color, fg="black", borderwidth = 1, font=(FontName, FontSize, "bold"), command=lambda nr = nr: Click(nr)) button.grid(row=y, column=x, sticky="nsew") button_row.append(button) buttons.append(button_row) root.grid_rowconfigure(7, weight=1) root.grid_columnconfigure(4, weight=1) return color= "light gray" FontName = "Verdana" FontSize = "14" root = tk.Tk() text = tk.Text(root, height = 1, width = 16, font = (FontName, FontSize, "bold")) TEKSTEN = ["exp","sin","cos","tan", "inv","kwad",":","x", "7","8","9","-", "4","5","6","+", "1","2","3","=", "Clear","0","Del","."] R = "" buttons = [] ShowButtons() root.mainloop()
Opmerking: na de aanpassing die in dit stuk code is gemaakt, zit er een kleine onhandigheid in het programma: zodra de "error" op het scherm gekomen is, werkt de "eXit" knop niet meer. Pas nadat je op "Clear" hebt gedrukt, werkt die knop weer.