def build(self, tk, parent): parent.setLayout(GridLayout(rows=3, columns=2)) parent.append(StaticText(tk, "Single")) parent.append(StaticText(tk, "Multiple")) l = List(tk, ((1, "Hello"), (2, "World"), (3, "Bye"))) l2 = List(tk, ((1, "One"), (2, "Two"), (3, "Three"), (4, "Four"), (5, "Five")), multiple=True) parent.append(l) parent.append(l2) out1 = StaticText(tk, str(l.value)) out2 = StaticText(tk, str(l2.value)) def click1(event): out1.text = str(l.value) def click2(event): out2.text = str(l2.value) l.click = click1 l2.click = click2 parent.append(out1) parent.append(out2) ## Add some extra's l.add(4, "almost last one") l.add(5, "Last one") ## make selection l.value = [3] l2.value = [1, 3, 5] out1.text = str(l.value) out2.text = str(l2.value)
def build(self, tk, parent): parent.setLayout(VBox()) text1 = Text(tk, value="Hello") text2 = Text(tk, rows=10, columns=40, value="World") s1 = StaticText(tk, "You typed: " + text1.value) s2 = StaticText(tk, "You typed: " + text2.value) def text1_changed(e): s1.text = "You typed: " + text1.value def text1_submitted(e): s1.text = "Submit!" def text2_changed(e): s2.text = "You typed: " + text2.value text1.keypress = text1_changed text1.submit = text1_submitted text2.keypress = text2_changed parent.append(text1) parent.append(s1) parent.append(text2) parent.append(s2)
def build(self, tk, parent): parent.setLayout(StaticVBox()) parent.append(StaticText(tk, "No expanding\n(centered)", background="yellow")) parent.append(StaticText(tk, "Horizontal", background="orange"), sticky=StaticVBox.EAST|StaticVBox.WEST) parent.append(StaticText(tk, "V\ne\nr\nt\ni\nc\na\nl", background="yellow"), sticky=StaticVBox.NORTH|StaticVBox.SOUTH) parent.append(StaticText(tk, "Horizontal\nV\ne\nr\nt\ni\nc\na\nl", background="orange"), sticky=StaticVBox.NEWS) parent.layout()
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): 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): d = Date(tk) s = StaticText(tk, "No date selected") def change(e): s.text = "Date selected: " + d.value d.change = change parent.append(d) parent.append(s)
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): parent.setLayout(GridLayout(columns=3, padx=2, pady=3)) parent.append(StaticText(tk, "Win 1", background="red"), colspan=2) parent.append(StaticText(tk, "Win 2", background="yellow")) parent.append(StaticText(tk, "Win 3", background="green"), rowspan=2) parent.append(StaticText(tk, "Win 4", background="orange")) parent.append(StaticText(tk, "Win 5", background="blue")) parent.append(StaticText(tk, "Win 6", background="pink")) parent.append(StaticText(tk, "Win 7", background="grey")) parent.append(StaticText(tk, "Win 8", background="brown"), rowspan=2, colspan=2) parent.layout()
def open_modal(self, e): if self.modal_window: self.modal_window.shut() self.modal_window = None else: self.modal_window = Window(self.tk, "Modal window", modal=True) self.modal_window.append(StaticText(self.tk, "I'm a modal window")) self.modal_window.open() def handle_close(e): self.modal_window = None self.modal_window.close = handle_close
def build(self, tk, parent): options = ((1, "Hello"), (2, "World"), (3, "Bye"), (4, "Last one")) dropdown = Dropdown(tk, options[:-1]) msg = StaticText(tk, "Make a selection") parent.append(dropdown) parent.append(msg) dropdown.add(*options[-1]) def click(e): msg.text = "You clicked " + dict(options)[dropdown.value] dropdown.click = click ## select the item with key 2 - "World" dropdown.value = 2
def build(self, tk, parent): parent.setLayout( GridLayout(columns=3, static=True, rows=3, padx=1, pady=1)) parent.append(StaticText(tk, "NW", background="yellow"), sticky=GridLayout.NORTH | GridLayout.WEST) parent.append(StaticText(tk, "N", background="orange"), sticky=GridLayout.NORTH) parent.append(StaticText(tk, "NE", background="yellow"), sticky=GridLayout.NORTH | GridLayout.EAST) parent.append(StaticText(tk, "W", background="orange"), sticky=GridLayout.WEST) parent.append(StaticText(tk, "X\nCENTER\nX", background="red"), sticky=GridLayout.CENTER) parent.append(StaticText(tk, "E", background="orange"), sticky=GridLayout.EAST) parent.append(StaticText(tk, "SW", background="yellow"), sticky=GridLayout.SOUTH | GridLayout.WEST) parent.append(StaticText(tk, "S", background="orange"), sticky=GridLayout.SOUTH) parent.append(StaticText(tk, "SE", background="yellow"), sticky=GridLayout.SOUTH | GridLayout.EAST) parent.layout()
def build(self, tk, parent): parent.setLayout(VBox()) b1 = RadioButton(tk, text="Option 1", value=1) b2 = RadioButton(tk, text="Option 2", value=2) b3 = RadioButton(tk, text="Option 3", value=3) b4 = RadioButton(tk, text="Option 4", value=4) parent.append(b1) parent.append(b2) parent.append(b3) parent.append(b4) def handle_group(e): selection.text = "Selected: %d" % group.value group = RadioGroup(b1, b2, b3, b4) group.selected = b3 group.click = handle_group selection = StaticText(tk, text="Selected: %d" % group.value) parent.append(selection)
def build(self, tk, parent): parent.setLayout(VBox()) box1 = CheckBox(tk, text="Option 1") box2 = CheckBox(tk, checked=True, text="Option 2") box3 = CheckBox(tk, text="Option 3") box4 = CheckBox(tk, text="Option 4") parent.append(box1) parent.append(box2) parent.append(box3) parent.append(box4) box4.checked = True selection = StaticText(tk) parent.append(selection) def handle_click(e): selection.text = ", ".join(b.text for b in (box1, box2, box3, box4) if b.checked) box1.click = box2.click = box3.click = box4.click = handle_click handle_click(None)
def open_a_lot(self, e): def hide_all(): while self.lots: w = self.lots.pop() w.shut() w.destroy() if self.lots: hide_all() return for p in (("left", "top"), ("center", "top"), ("right", "top"), ("left", "center"), ("center", ), ("right", "center"), ("left", "bottom"), ("center", "bottom"), ("right", "bottom")): l = ", ".join(p) w = Window(self.tk, l + " window", position=p) w.append(StaticText(self.tk, l)) w.open() w.close = lambda e: hide_all() self.lots.append(w)
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()
def build(self, tk, parent): static = StaticText(tk, "StaticText just displays some text") parent.append(static)