def build(self, tk, parent): self.running = False resources = [ addResource(FileResource("resources/nyancat-%d.png" % i, name="nyancat-%d" % i, type="image/png")) for i in range(0, 12)] img = Image(tk, resource=resources[0]) button = Button(tk, "Start") parent.append(img) parent.append(button) def update_image(i): if self.running: img.resource = resources[i] tk.set_timer(lambda e: update_image((i+1) % 12), 250) def startstop(e): button.text = self.running and "Start" or "Stop" self.running = not self.running update_image(0) button.click = startstop
def build(self, tk, parent): button = Button(tk, "Click me!") def click(e): button.text = "Click me again!" button.click = click parent.append(button)
def build(self, tk, parent): button = Button(tk, "Click me") self.message = StaticText(tk, "0 clicks") button.click = self.click_handler parent.append(self.message) parent.append(button)
def build(self, tk, parent): parent.setLayout(HBox()) p1 = Panel(tk,width=200, height=200) p2 = Panel(tk, scrolling=True, width=200, height=200) p2.setLayout(VBox()) p1.append(StaticText(tk, "Non-scrolling panel")) p2.append(StaticText(tk, "Scrolling panel")) b = Button(tk, "Click me") p1.append(b) def click(e): p2.append(StaticText(tk, "Line added")) parent.layout() p2.scrollbottom() b.click = click parent.append(p1) parent.append(p2)
def build(self, tk, parent): self.tk = tk parent.setLayout(GridLayout(columns=2)) self.startstop = Button(tk, "Start") self.message = StaticText(tk, "0 events") self.startstop.click = self.startstop_handler parent.append(self.message) parent.append(self.startstop)
def build(self, tk, parent): parent.setLayout(Grid(rows=2, columns=4, expand_vertical=True)) self.text = Text(tk) append_button = Button(tk, 'Append') append_button.click = self.append remove_button = Button(tk, 'Remove') remove_button.click = self.remove clear_button = Button(tk, 'Clear') clear_button.click = self.clear parent.append(self.text) parent.append(append_button) parent.append(remove_button) parent.append(clear_button) self.collection = Collection(tk, StaticText) self.collection.extend(['Hello', 'World', 'Bye']) parent.append(self.collection)
def build(self, tk, parent): button = Button(tk, "Click me!") t1 = StaticText(tk, 'Enabled:') c1 = CheckBox(tk) t2 = StaticText(tk, 'Visible:') c2 = CheckBox(tk, checked=True) def toggle_state(x): button.enabled = not button.enabled c1.click = toggle_state def toggle_visibility(x): button.visible = c2.checked c2.click = toggle_visibility button.enabled = False parent.append(button) parent.append(t1) parent.append(c1) parent.append(t2) parent.append(c2)
def build(self, tk, parent): self.tk = tk self.lots = [] modal = Button(tk, "Click to open a modal window") nonmodal = Button(tk, "Click to open a non-modal window") alot = Button(tk, "Click to open a lot of windows") self.modal_window = None self.nonmodal_window = None modal.click = self.open_modal nonmodal.click = self.open_nonmodal alot.click = self.open_a_lot parent.append(modal) parent.append(nonmodal) parent.append(alot)
def build(self, tk, parent): self.running = False resources = [ addResource(FileResource("resources/nyancat-%d.png" % i, name="nyancat-%d" % i, type="image/png")) for i in range(0, 12) ] img = Image(tk, resource=resources[0]) button = Button(tk, "Start") parent.append(img) parent.append(button) def update_image(i): if self.running: img.resource = resources[i] tk.set_timer(lambda e: update_image((i + 1) % 12), 250) def startstop(e): button.text = self.running and "Start" or "Stop" self.running = not self.running update_image(0) button.click = startstop
class Calculator(object): title = "Calculator" description = "Sample calculator application" def build(self, tk, parent): def reset(): self.equation = "" self.digits = 0 self.floating = 0 self.in_float = False def val(): if self.in_float: return "%d.%d" % (self.digits, self.floating) return str(self.digits) def digit(num): if self.in_float: self.floating *= 10 self.floating += num self.display.text = val() else: self.digits *= 10 self.digits += num self.display.text = str(self.digits) def clear(e): reset() self.display.text = str(self.digits) def point(e): if not self.in_float: self.in_float = True self.display.text = str(self.digits) + "." def op(operator): self.equation += "%d.%d%s" % (self.digits, self.floating, operator) self.digits = 0 self.floating = 0 self.in_float = False def equals(e): self.equation += val() try: res = eval(self.equation) if res == int(res): self.display.text = "%d" % res else: self.display.text = "%f" % eval(self.equation) except ZeroDivisionError, e: self.display.text = "err" reset() reset() ## make it static, we want buttons to be equally sized. parent.setLayout( GridLayout(rows=6, columns=4, padx=1, pady=1, static=True, sticky=GridLayout.NEWS)) self.display = StaticText(tk, str(self.digits), background="black", foreground="white") ## buttons 0 .. 9 for n in range(0, 10): b = Button(tk, str(n)) b.click = lambda e, n=n: digit(n) setattr(self, "b%d" % n, b) self.clear = Button(tk, "C") self.dummy = StaticText(tk, " ") self.divide = Button(tk, "/") self.multiply = Button(tk, "x") self.plus = Button(tk, "+") self.substract = Button(tk, "-") self.equals = Button(tk, "=") self.point = Button(tk, ".") self.clear.click = clear self.point.click = point self.plus.click = lambda e: op("+") self.substract.click = lambda e: op("-") self.multiply.click = lambda e: op("*") self.divide.click = lambda e: op("/") self.equals.click = equals parent.append(self.display, row=0, column=0, colspan=4) # C _ / * parent.append(self.clear, row=1, column=0) parent.append(self.dummy, row=1, column=1) parent.append(self.divide, row=1, column=2) parent.append(self.multiply, row=1, column=3) # 7 8 9 - parent.append(self.b7, row=2, column=0) parent.append(self.b8, row=2, column=1) parent.append(self.b9, row=2, column=2) parent.append(self.substract, row=2, column=3) # 4 5 6 + parent.append(self.b4, row=3, column=0) parent.append(self.b5, row=3, column=1) parent.append(self.b6, row=3, column=2) parent.append(self.plus, row=3, column=3) # 1 2 3 = parent.append(self.b1, row=4, column=0) parent.append(self.b2, row=4, column=1) parent.append(self.b3, row=4, column=2) parent.append(self.equals, row=4, column=3, rowspan=2) # 0 . parent.append(self.b0, row=5, column=0, colspan=2) parent.append(self.point, row=5, column=2) parent.layout()