Aufgabenstellung
'Spukschloss'
Museum 0
# -*- coding: iso-8859-1 -*-
# mk, 18.1.09
# model
class Zaehler():
def __init__(self,callback=None):
self.__n = 0
self.__callback = callback
def setCallback(self,callback):
self.__callback = callback
def setN(self,n):
self.__n = n
if self.__callback != None:
self.__callback(self.getN())
def getN(self):
return self.__n
def auf(self):
self.__n = self.__n + 1
if self.__callback != None:
self.__callback(self.getN())
def ab(self):
self.__n = self.__n - 1
if self.__callback != None:
self.__callback(self.getN())
# control
z = Zaehler()
def cbUp():
z.auf()
def cbDown():
z.ab()
def update(n):
a1.l.config(text=str(n))
a2.l.config(text=str(n))
z.setCallback(update)
# view
import Tkinter as tk
e1 = tk.Tk()
e1.title('Eingabe 1')
e1.geometry('200x100+10+10')
e1.bUp = tk.Button(master=e1,text='auf',width='10',command=cbUp)
e1.bUp.place(x=20,y=20)
e1.bDown = tk.Button(master=e1,text='ab',width='10',command=cbDown)
e1.bDown.place(x=20,y=50)
e2 = tk.Tk()
e2.title('Eingabe 2')
e2.geometry('200x100+230+10')
e2.bUp = tk.Button(master=e2,text='auf',width='10',command=cbUp)
e2.bUp.place(x=20,y=20)
e2.bDown = tk.Button(master=e2,text='ab',width='10',command=cbDown)
e2.bDown.place(x=20,y=50)
a1 = tk.Tk()
a1.title('Ausgabe 1')
a1.geometry('200x100+10+200')
a1.l = tk.Label(master=a1,text='spam',font=('Arial', 50))
a1.l.place(x=20,y=10)
a2 = tk.Tk()
a2.title('Ausgabe 2')
a2.geometry('200x100+230+200')
a2.l = tk.Label(master=a2,text='eggs',font=('Arial', 50))
a2.l.place(x=20,y=10)
tk.mainloop()
Aufgabe
Versuche, jede Einzelheit des Programms zu verstehen.
Füge weitere Ein- und Ausgabefenster ein. Was ist an dem Programm gut, was ist nicht so gut?
Museum 1
# -*- coding: iso-8859-1 -*-
# mk, 20.1.09
# model
class Zaehler():
def __init__(self,callback=None):
self.__n = 0
self.__callback = callback
def setCallback(self,callback):
self.__callback = callback
def setN(self,n):
self.__n = n
if self.__callback != None:
self.__callback(self.getN())
def getN(self):
return self.__n
def auf(self):
self.__n = self.__n + 1
if self.__callback != None:
self.__callback(self.getN())
def ab(self):
self.__n = self.__n - 1
if self.__callback != None:
self.__callback(self.getN())
# view
import Tkinter as tk
class Eingabe(object):
def __init__(self,nr,cbUp,cbDown):
self.__root = tk.Tk()
self.__root.title('Eingabe '+str(nr))
self.__root.geometry('200x100+'+str(10+nr*210)+'+10')
self.__root.bUp = tk.Button(master=self.__root,
text='auf',width='10',command=cbUp)
self.__root.bUp.place(x=20,y=20)
self.__root.bDown = tk.Button(master=self.__root,
text='ab',width='10',command=cbDown)
self.__root.bDown.place(x=20,y=50)
class Ausgabe(object):
def __init__(self,nr):
self.__root = tk.Tk()
self.__root.title('Ausgabe '+str(nr))
self.__root.geometry('200x100+'+str(10+nr*210)+'+200')
self.__root.l = tk.Label(master=self.__root,
text='__',font=('Arial', 50))
self.__root.l.place(x=20,y=10)
def update(self,n):
self.__root.l.config(text=str(n))
# control
z = Zaehler()
def cbUp():
z.auf()
def cbDown():
z.ab()
e1 = Eingabe(1,cbUp,cbDown)
e2 = Eingabe(2,cbUp,cbDown)
a1 = Ausgabe(1)
a2 = Ausgabe(2)
def update(n):
a1.update(n)
a2.update(n)
z.setCallback(update)
tk.mainloop()
WM_Demo.py