class InputBox(UIBase): def __init__(self, canvas, point, input_type, label_text, char_max=10, default_val=None): allowed_types = ['unsigned_int', 'unsigned_float', 'float'] if input_type not in allowed_types: raise Exception('InputBox: type given is not an allowed type') self.canvas = canvas self.point = point self.type = input_type self.label = Text(point, label_text) x_offset = 120 + ((char_max - 10)/2.0 * 9) # diff b/t default char_max and font_size / 2 self.entry = Entry(Point(point.x + x_offset, point.y), char_max) if default_val is not None: self.set_input(default_val) self.draw() @property def input_text(self): return self.entry.getText() def validate_input(self): flag = True if self.type == 'unsigned_int': flag = isinstance(int(self.input_text), int) and int(self.input_text) > 0 elif self.type == 'unsigned_float': flag = isinstance(float(self.input_text), float) and float(self.input_text) > 0 elif self.type == 'float': flag = isinstance(float(self.input_text), float) if not flag: self.label.setTextColor('red') else: self.label.setTextColor('black') return flag def set_input(self, val): self.entry.setText(val) if self.validate_input() is not True: self.entry.setText('') def draw(self): self.label.draw(self.canvas) self.entry.draw(self.canvas) def undraw(self): self.label.undraw() self.entry.undraw() def get_point_with_offset(self): return Point(self.point.x, self.point.y + 30)
def graphics(): dict = { 'jsmith': '1994', 'rstein': '2010', 'rong': '1945', 'annah': '2020' } holder = { 'jsmith': 'John Smith', 'rstein': 'Rebecca Stein', 'rong': 'Ronald Gray', 'annah': 'Anna Heller' } from graphics import GraphWin, Rectangle, Entry, Text, Point, Circle win = GraphWin('Automated Transaction Machine', 500, 500) win.setCoords(0, 0, 40, 40) Welcome_message = Text(Point(20, 35), 'Welcome to the Automated Transaction Machine') Rect = Rectangle(Point(2, 2), Point(37, 37)) Rect.draw(win) Welcome_message.draw(win) Pin_value = Text(Point(10, 32), '\tPlease input your 1.UserID and 2. PIN below') Pin_value.setSize(10) Pin_value.draw(win) userID = Entry(Point(8, 30), 8) userID.draw(win) pin = Entry(Point(8, 26), 8) pin.draw(win) win.getMouse() x = pin.getText() y = userID.getText() Pin_value.undraw() userID.undraw() pin.undraw() while True: try: holder = CheckingAccount(y, x) break except ValueError: Incorrect_value = Text(Point(10, 32), '\tYour PIN is incorrect. Try Again') Incorrect_value.draw(win) pin = Entry(Point(8, 26), 8) pin.draw(win) win.getMouse() x = pin.getText() Incorrect_value.undraw() pin.undraw() except NameError: Incorrect_value = Text(Point(10, 32), '\tYour UserID is incorrect. Try Again') Incorrect_value.draw(win) userID = Entry(Point(8, 30), 8) userID.draw(win) win.getMouse() y = userID.getText() Incorrect_value.undraw() userID.undraw() userID.undraw() pin.undraw() Welcome = Text( Point(20, 25), "Welcome " + str(holder) + "! It is a pleasure serving you today!") Welcome.draw(win) win.getMouse() Welcome.undraw() Option = Text(Point(20, 25), 'Would you like to withdraw or deposit') Option.draw(win) win.getMouse()