Verfasst: Donnerstag 15. Februar 2007, 13:56
Du hast recht set brauche ich gar nicht - nur kann ich nicht sagen warum es vorher nicht funktioniert hat ???? na -egal es geht auch ohne!
Seit 2002 Diskussionen rund um die Programmiersprache Python
https://www.python-forum.de/
Code: Alles auswählen
# -*- coding: iso-8859-15 -*-
from Tkinter import *
class Rechner (Frame):
def __init__(self, master=None):
self.speicher = ""
self.mspeicher = ""
Frame.__init__(self, master,)
self.pack()
self.buttonsieben = Button(self, text="7",width=3, command=self.eingabesieben)
self.buttonsieben.grid(row=7, column=1,padx=10)
self.buttonacht = Button(self,text="8",width=3, command=self.eingabeacht)
self.buttonacht.grid(row=7, column=2)
self.buttonneun = Button(self, text="9",width=3, command=self.eingabeneun)
self.buttonneun.grid(row=7, column=3,padx=10)
self.buttonneun = Button(self, text="/",width=3, command=self.eingabedivi)
self.buttonneun.grid(row=7, column=4)
self.buttonloeschen = Button(self, text="C",width=3, command=self.loeschen)
self.buttonloeschen.grid(row=7, column=5,padx=10)
self.buttonvier = Button(self, text="4",width=3, command=self.eingabevier)
self.buttonvier.grid(row=8, column=1,pady=10)
self.buttonfuenf = Button(self, text="5",width=3, command=self.eingabefuenf)
self.buttonfuenf.grid(row=8, column=2)
self.buttonsechs = Button(self, text="6",width=3, command=self.eingabesechs)
self.buttonsechs.grid(row=8, column=3,)
self.buttonmulti = Button(self, text="X",width=3, command=self.eingabemulti)
self.buttonmulti.grid(row=8, column=4)
self.buttonprozent = Button(self, text="%",width=3, command=self.eingabeprozent)
self.buttonprozent.grid(row=8, column=5,)
self.buttoneins = Button(self, text="1",width=3, command=self.eingabeeins)
self.buttoneins.grid(row=9, column=1)
self.buttonzwei = Button(self, text="2",width=3, command=self.eingabezwei)
self.buttonzwei.grid(row=9, column=2)
self.buttondrei = Button(self, text="3",width=3, command=self.eingabedrei)
self.buttondrei.grid(row=9, column=3)
self.buttonminus = Button(self, text="-",width=3, command=self.eingabeminus)
self.buttonminus.grid(row=9, column=4)
self.buttonspeicher = Button(self, text="M",width=3, command=self.eingabemspeicher)
self.buttonspeicher.grid(row=9, column=5)
self.buttonnull = Button(self, text="0",width=3, command=self.eingabenull)
self.buttonnull.grid(row=10, column=1,pady=10)
self.buttonplusminus = Button(self, text="+/-",width=3, command=self.eingabevorzeichen)
self.buttonplusminus.grid(row=10, column=2,)
self.buttonkomma = Button(self, text=",",width=3, command=self.eingabekomma)
self.buttonkomma.grid(row=10, column=3)
self.buttonplus = Button(self, text="+",width=3, command=self.eingabeplus)
self.buttonplus.grid(row=10, column=4)
self.buttongleich = Button(self, text="=",width=3, command=self.eingabegleich)
self.buttongleich.grid(row=10, column=5)
self.ausgabe = Entry(self,width=29,justify="right",)
self.ausgabe.grid(row=0, column=0,columnspan=6,pady=10)
def eingabeeins(self):
self.ausgabe.insert(50,"1",)
def eingabezwei(self):
self.ausgabe.insert(50,"2")
def eingabedrei(self):
self.ausgabe.insert(50,"3")
def eingabeminus(self):
self.speicher = self.speicher + self.ausgabe.get() + "-"
self.ausgabe.delete(0,END)
def eingabemspeicher(self):
if self.ausgabe.get() =="":
self.speicher = self.speicher + self.mspeicher
if self.ausgabe.get() !="":
self.mspeicher = self.ausgabe.get()
self.ausgabe.delete(0,END)
self.ausgabe.insert(50,"")
def eingabenull(self):
self.ausgabe.insert(50,"0")
def eingabevorzeichen(self):
self.vorzeichen = str(float (self.ausgabe.get()) * -1)
self.ausgabe.delete(0,END)
self.ausgabe.insert(50,self.vorzeichen)
def eingabekomma(self):
self.ausgabe.insert(50,".")
def eingabeplus(self):
self.speicher = self.speicher + self.ausgabe.get()+ "+"
self.ausgabe.delete(0,END)
def eingabevier(self):
self.ausgabe.insert(50,"4")
def eingabefuenf(self):
self.ausgabe.insert(50,"5")
def eingabesechs(self):
self.ausgabe.insert(50,"6")
def eingabemulti(self):
self.speicher = self.ausgabe.get()+ "*"
self.ausgabe.delete(0,END)
def eingabeprozent(self):
self.speicher = self.ausgabe.get()
self.speicher = (self.speicher + "/" + "100" + "*")
self.ausgabe.delete(0,END)
def eingabegleich(self):
try:
self.speicher = str(eval (self.speicher + self.ausgabe.get()))
self.ausgabe.delete(0,END)
self.ausgabe.insert(50,self.speicher)
self.speicher = ""
except:
self.ausgabe.delete(0,END)
self.speicher = ""
self.ausgabe.insert(50,"E")
def eingabesieben(self):
self.ausgabe.insert(50,"7")
def eingabeacht(self):
self.ausgabe.insert(50,"8")
def eingabeneun(self):
self.ausgabe.insert(50,"9")
def eingabedivi(self):
self.speicher = self.speicher + self.ausgabe.get()+ "/"
self.ausgabe.delete(0,END)
def loeschen(self):
self.ausgabe.delete(0,END)
self.speicher = ""
self.mspeicher = ""
root = Tk()
root.title('mudda`s Rechner 1.0')
root.config(cursor='crosshair')
Rechner(root).mainloop()
Code: Alles auswählen
# -*- coding: iso-8859-15 -*-
from Tkinter import *
class Rechner (Frame):
def __init__(self, master=None):
tastenlayout = [("7",3,7,1,10,0),("8",3,7,2,0,0),("9",3,7,3,10,0),
("4",3,8,1,0,10),("5",3,8,2,0,0),("6",3,8,3,0,0),
("1",3,9,1,0,0),("2",3,9,2,0,0),("3",3,9,3,0,0)]
Frame.__init__(self, master,)
self.pack()
for text, width, row, column, padx, pady in tastenlayout:
self.tasten = Button(self, text=text,width=width, command=lambda:self.ausgabe.insert(50,text))
self.tasten.grid(row=row, column=column, padx=padx, pady=pady)
self.ausgabe = Entry(self,width=29,justify="right",)
self.ausgabe.grid(row=0, column=0,columnspan=6,pady=10)
root = Tk()
root.title('mudda`s Rechner 1.0')
root.config(cursor='crosshair')
Rechner(root).mainloop()
Code: Alles auswählen
self.tasten = Button(self, text=text,width=width,command=lambda self=self, t=text: self.ausgabe.insert(50,t))
Code: Alles auswählen
# -*- coding: iso-8859-15 -*-
from Tkinter import *
class Rechner (Frame):
def __init__(self, master=None):
tastenlayout = [("7",3,7,1,10,0),("8",3,7,2,0,0),("9",3,7,3,10,0),("4",3,8,1,0,10),
("5",3,8,2,0,0),("6",3,8,3,0,0),("1",3,9,1,0,0),("2",3,9,2,0,0),
("3",3,9,3,0,0),("0",3,10,1,0,10),(",",3,10,3,0,0)]
funktastlayout = [("/",3,7,4,0,0),("X",3,8,4,0,0,),("-",3,9,4,0,0),("+",3,10,4,0,0)]
speicher=""
Frame.__init__(self, master,)
self.pack()
for text, width, row, column, padx, pady in tastenlayout:
self.tasten = Button(self, text=text,width=width, command=lambda t=text: self.ausgabe.insert(50,t))
self.tasten.grid(row=row, column=column, padx=padx, pady=pady)
for text, width, row, column, padx, pady in funktastlayout:
self.funktasten = Button(self, text=text,width=width, command=lambda speicher=speicher+self.ausgabe.get()+ text : self.ausgabe.delete(0,END))
self.funktasten.grid(row=row, column=column, padx=padx, pady=pady)
self.ausgabe = Entry(self,width=29,justify="right",)
self.ausgabe.grid(row=0, column=0,columnspan=6,pady=10)
root = Tk()
root.title('mudda`s Rechner 1.0')
root.config(cursor='crosshair')
Rechner(root).mainloop()
Code: Alles auswählen
def _make_operator_command(self, text):
def operator_command():
self.speicher += self.ausgabe.get() + text
self.ausgabe.delete(0, END)
return operator_command
Code: Alles auswählen
In [46]: def make_adder(number):
....: def add(n):
....: return number + n
....: return add
....:
In [47]: add5 = make_adder(5)
In [48]: add5(0)
Out[48]: 5
In [49]: add5(10)
Out[49]: 15
In [50]: add5(42)
Out[50]: 47
Mein "richtiger Taschenrechner" Zeigt auch Rechenzeichen an - finde ich sehr praktisch, weil ich die Eingaben editieren kann.kaytec hat geschrieben:doch bei meinem wollte ich nicht, dass die Zeichen +,- etc. im Rechenvorgang zusehen sind, sondern es wie ein richtiger taschenrechener funktioniert.
Code: Alles auswählen
self.funktasten = Button(self, text=text,width=width, command=lambda text=text:self._make_operator_command)
self.funktasten.grid(row=row, column=column, padx=padx, pady=pady)
self.ausgabe = Entry(self,width=29,justify="right",)
self.ausgabe.grid(row=0, column=0,columnspan=6,pady=10)
def _make_operator_command(self, text):
def operator_command():
self.speicher += self.ausgabe.get() + text
self.ausgabe.delete(0, END)
return operator_command
Code: Alles auswählen
taste = Button(..., command=self._make_operator_command(text))