Grafischer Taschenrechner Tkinter Prozentrechnungsproblem
Verfasst: Donnerstag 8. April 2021, 12:30
Die Problemstellen sind Markiert. Ich bräuchte Hilfe wie man die Prozentrechnung in diesem Skript gut implementieren kann. Danke im Voraus!
# [tkinter Import]
from tkinter import *
# [Fenster erstellen]
fenster = Tk()
fenster.title("Rechner")
#fenster.iconbitmap(r"Taschenrechner.ico")
operator = ""
text_Input = StringVar()
ProzentVariabel = False
# [Buttonfunktionen]
# Zahlen & Rechenzeichen Clickfunktion
def ButtonClick(numbers):
global operator
operator = operator + str(numbers)
text_Input.set(operator)
# Clear Funktion
def ButtonClear():
global operator
operator = ""
text_Input.set("")
# Gleichheits Funktion
def ButtonGleich():
global operator
RechnungListe = operator.split()
Zahl2 = RechnungListe[1:]
ZweiteZahlVariabel = True
if ProzentVariabel == True:
einProzent = Zahl1 / 100
while ZweiteZahlVariabel == False:
einProzent = einProzent
ProzentZahlFertig = einProzent * Zahl2
sumup = str(eval(operator))
text_Input.set(sumup)
operator = ""
else:
sumup = str(eval(operator))
text_Input.set(sumup)
operator = ""
# Hoch² Funktion
def ButtonHoch(numbers):
global operator
text_Input.set(operator + " ² ")
operator = operator + str(numbers)
# Wurzel Funktion
def ButtonQuadratWurzel(numbers):
global operator
text_Input.set(" √ " + operator)
operator = operator + str(numbers)
def ButtonProzentrechnung(numbers):
global operator
Zahl1 = operator
text_Input.set(operator + " von ")
operator = operator + str(numbers)
ProzentVariabel = True
# [Main Display Top]
textDisplay = Entry(fenster, font = ("arial", 20, "bold"), textvariabl = text_Input, bd = 30, insertwidth = 4, bg = "Dark grey", justify = "right").grid(columnspan = 4)
# [Zahlen Buttons]
# Buttons 7-9
button7 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "7", command = lambda:ButtonClick(7), bg = "Dark grey").grid(row = 2, column = 0)
button8 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "8", command = lambda:ButtonClick(8), bg = "Dark grey").grid(row = 2, column = 1)
button9 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "9", command = lambda:ButtonClick(9), bg = "Dark grey").grid(row = 2, column = 2)
# Buttons 4-6
button4 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "4", command = lambda:ButtonClick(4), bg = "Dark grey").grid(row = 3, column = 0)
button5 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "5", command = lambda:ButtonClick(5), bg = "Dark grey").grid(row = 3, column = 1)
button6 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "6", command = lambda:ButtonClick(6), bg = "Dark grey").grid(row = 3, column = 2)
# Buttons 1-3
button1 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "1", command = lambda:ButtonClick(1), bg = "Dark grey").grid(row = 4, column = 0)
button2 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "2", command = lambda:ButtonClick(2), bg = "Dark grey").grid(row = 4, column = 1)
button3 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "3", command = lambda:ButtonClick(3), bg = "Dark grey").grid(row = 4, column = 2)
# Button 0
button0 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "0", command = lambda:ButtonClick(0), bg = "Dark grey").grid(row = 5, column = 1)
# [Rechenzeichenbuttons]
# Button Prozent
ButtonProzent = Button(fenster, padx = 11, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "%", command = lambda:ButtonProzentrechnung(" von "), bg = "grey").grid(row = 1, column = 1)
# Button Wurzel
ButtonWurzel = Button(fenster, padx = 15, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "√", command = lambda:ButtonQuadratWurzel(" ** 0.5 "), bg = "grey").grid(row = 1, column = 0)
# Button Hoch²
ButtonHoch2 = Button(fenster, padx = 12, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "italic", "bold"), text = "x²", command = lambda:ButtonHoch(" ** 2 "), bg = "grey").grid(row = 1, column = 2)
# Button Geteilt
ButtonDivision = Button(fenster, padx = 15, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "÷", command = lambda:ButtonClick(" / "), bg = "grey").grid(row = 1, column = 3)
# Button Mal
ButtonMultiplikation = Button(fenster, padx = 17, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "*", command = lambda:ButtonClick(" * "), bg = "grey").grid(row = 2, column = 3)
# Button Minus
ButtonSubtraktion = Button(fenster, padx = 18, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "-", command = lambda:ButtonClick(" - "), bg = "grey").grid(row = 3, column = 3)
# Button Plus
ButtonAddition = Button(fenster, padx = 14, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "+", command = lambda:ButtonClick(" + "), bg = "grey").grid(row = 4, column = 3)
# Button Gleich
ButtonGleich = Button(fenster, padx = 14, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "=", command = ButtonGleich, bg = "light blue").grid(row = 5, column = 3)
#Button Clear
ButtonClear = Button(fenster, padx = 14, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "C", command = ButtonClear, bg = "Dark grey").grid(row = 5, column = 0)
# Button Komma
ButtonKomma = Button(fenster, padx = 20, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = ",", command = lambda:ButtonClick("."), bg = "Dark grey").grid(row = 5, column = 2)
# [Fenster Loop]
fenster.mainloop()
# [tkinter Import]
from tkinter import *
# [Fenster erstellen]
fenster = Tk()
fenster.title("Rechner")
#fenster.iconbitmap(r"Taschenrechner.ico")
operator = ""
text_Input = StringVar()
ProzentVariabel = False
# [Buttonfunktionen]
# Zahlen & Rechenzeichen Clickfunktion
def ButtonClick(numbers):
global operator
operator = operator + str(numbers)
text_Input.set(operator)
# Clear Funktion
def ButtonClear():
global operator
operator = ""
text_Input.set("")
# Gleichheits Funktion
def ButtonGleich():
global operator
RechnungListe = operator.split()
Zahl2 = RechnungListe[1:]
ZweiteZahlVariabel = True
if ProzentVariabel == True:
einProzent = Zahl1 / 100
while ZweiteZahlVariabel == False:
einProzent = einProzent
ProzentZahlFertig = einProzent * Zahl2
sumup = str(eval(operator))
text_Input.set(sumup)
operator = ""
else:
sumup = str(eval(operator))
text_Input.set(sumup)
operator = ""
# Hoch² Funktion
def ButtonHoch(numbers):
global operator
text_Input.set(operator + " ² ")
operator = operator + str(numbers)
# Wurzel Funktion
def ButtonQuadratWurzel(numbers):
global operator
text_Input.set(" √ " + operator)
operator = operator + str(numbers)
def ButtonProzentrechnung(numbers):
global operator
Zahl1 = operator
text_Input.set(operator + " von ")
operator = operator + str(numbers)
ProzentVariabel = True
# [Main Display Top]
textDisplay = Entry(fenster, font = ("arial", 20, "bold"), textvariabl = text_Input, bd = 30, insertwidth = 4, bg = "Dark grey", justify = "right").grid(columnspan = 4)
# [Zahlen Buttons]
# Buttons 7-9
button7 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "7", command = lambda:ButtonClick(7), bg = "Dark grey").grid(row = 2, column = 0)
button8 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "8", command = lambda:ButtonClick(8), bg = "Dark grey").grid(row = 2, column = 1)
button9 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "9", command = lambda:ButtonClick(9), bg = "Dark grey").grid(row = 2, column = 2)
# Buttons 4-6
button4 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "4", command = lambda:ButtonClick(4), bg = "Dark grey").grid(row = 3, column = 0)
button5 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "5", command = lambda:ButtonClick(5), bg = "Dark grey").grid(row = 3, column = 1)
button6 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "6", command = lambda:ButtonClick(6), bg = "Dark grey").grid(row = 3, column = 2)
# Buttons 1-3
button1 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "1", command = lambda:ButtonClick(1), bg = "Dark grey").grid(row = 4, column = 0)
button2 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "2", command = lambda:ButtonClick(2), bg = "Dark grey").grid(row = 4, column = 1)
button3 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "3", command = lambda:ButtonClick(3), bg = "Dark grey").grid(row = 4, column = 2)
# Button 0
button0 = Button(fenster, padx = 16, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "0", command = lambda:ButtonClick(0), bg = "Dark grey").grid(row = 5, column = 1)
# [Rechenzeichenbuttons]
# Button Prozent
ButtonProzent = Button(fenster, padx = 11, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "%", command = lambda:ButtonProzentrechnung(" von "), bg = "grey").grid(row = 1, column = 1)
# Button Wurzel
ButtonWurzel = Button(fenster, padx = 15, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "√", command = lambda:ButtonQuadratWurzel(" ** 0.5 "), bg = "grey").grid(row = 1, column = 0)
# Button Hoch²
ButtonHoch2 = Button(fenster, padx = 12, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "italic", "bold"), text = "x²", command = lambda:ButtonHoch(" ** 2 "), bg = "grey").grid(row = 1, column = 2)
# Button Geteilt
ButtonDivision = Button(fenster, padx = 15, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "÷", command = lambda:ButtonClick(" / "), bg = "grey").grid(row = 1, column = 3)
# Button Mal
ButtonMultiplikation = Button(fenster, padx = 17, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "*", command = lambda:ButtonClick(" * "), bg = "grey").grid(row = 2, column = 3)
# Button Minus
ButtonSubtraktion = Button(fenster, padx = 18, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "-", command = lambda:ButtonClick(" - "), bg = "grey").grid(row = 3, column = 3)
# Button Plus
ButtonAddition = Button(fenster, padx = 14, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "+", command = lambda:ButtonClick(" + "), bg = "grey").grid(row = 4, column = 3)
# Button Gleich
ButtonGleich = Button(fenster, padx = 14, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "=", command = ButtonGleich, bg = "light blue").grid(row = 5, column = 3)
#Button Clear
ButtonClear = Button(fenster, padx = 14, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = "C", command = ButtonClear, bg = "Dark grey").grid(row = 5, column = 0)
# Button Komma
ButtonKomma = Button(fenster, padx = 20, pady = 16, bd = 8, fg = "Black", font = ("arial", 20, "bold"), text = ",", command = lambda:ButtonClick("."), bg = "Dark grey").grid(row = 5, column = 2)
# [Fenster Loop]
fenster.mainloop()