def setup(sk): # Load image originals fldr = resolvePath("img", __file__) sk.imgs = [ Image("{}/{}.png".format(fldr, f)) for f in ("target", "paper", "scissors") ] # Create button controls and add images to buttons w, h = sk.size x = w / 4 - 44 for img in sk.imgs: btn = Button((40, 40), 2).config(anchor=BOTTOM, pos=(x, h - 4)) btn += Image(img).config(pos=btn.center, height=32) sk += btn.bind(onclick) # Bind event handlers x += 44 # Initialize scores to zero font = {"font": Font.mono(), "fontSize": 40, "fontStyle": BOLD} sk["Score1"] = Text(0).config(color="red", anchor=TOPLEFT, pos=(4, 4), **font) sk["Score2"] = Text(0).config(color="blue", anchor=TOPRIGHT, pos=(w - 4, 4), **font) # Create status text sk["Status"] = Text("Make a choice!").config(pos=(0.75 * w, h - 24), font=Font.sans(), fontSize=32, color="red")
def __init__(self, items, size=(192, 24), weight=1, padding=6, options=2, txtConfig={}): w, h = size x = y = weight if "fontSize" not in txtConfig: tmp = dict(fontSize=h - padding) tmp.update(txtConfig) txtConfig = tmp if type(items) is int: n = items items = None else: n = len(items) buttons = [] for i in range(n): b = Button(size, options).config(anchor=TOPLEFT, pos=(x, y), weight=0) buttons.append(b) y += h if items: self._item(b, items[i], padding) for gr in b: if isinstance(gr, Text): gr.config(**txtConfig) w += 2 * weight h = n * size[1] + 2 * weight super().__init__((w, h), buttons[0].options[0]) self.config(weight=weight) self += buttons
def buttons(cfg): "Create a canvas containing two buttons" cv = Canvas((256, 48)) # Selectable button btn1 = Button((120, 48)).textIcon("Selectable\nButton", True) cv["Selectable"] = btn1.config(anchor=TOPLEFT).bind(onaction=buttonClick) # Non-selectable button btn2 = Button(btn1.size, 2).textIcon("Popup Menu").bind(onaction=buttonClick) cv["Popup"] = btn2.config(pos=(255, 0), anchor=TOPRIGHT) # Configure button text btn1[-1].config(color=BLUE, align=CENTER, **cfg) btn2[-1].config(**cfg) return cv
def recordButton(self, y): "Compose the record button" sz = 21, 21 btn = Button(sz, ["#ffffff00", "#ffc0c0"]).config(anchor=LEFT, pos=(12, y), weight=0).bind(onclick) img = Circle(76).config(fill="red", weight=6).snapshot(), Image( (19, 19)) btn += Sprite(img).config(pos=btn.center, height=19) return btn
def __init__(self, text, userInput=None, buttons=None, title=None, size=(1, 1), inputWidth=None, **kwargs): super().__init__(size) self.command = None # Text options txtConfig = {"font": Font.sans(), "fontSize": 15} txtConfig.update(kwargs) # Compose button text if buttons is None: buttons = ["Okay", "Cancel"] elif type(buttons) is str: buttons = buttons, if len(buttons) > 2: buttons = buttons[:2] # Add buttons bSize = None icon = True for t in buttons: t = Text(t).config(**txtConfig) if not bSize: bSize = 0, t.height + 12 name = "Button_{}".format(t.data) self[name] = Button(bSize, 2).textIcon(t, icon).config(anchor=BOTTOM) icon = not icon self.buttons = self[:len(buttons)] for b in self.buttons: b.bind(onaction=_btnClick) # Add text label and text input self["Text"] = text = Text(text).config(anchor=TOP, **txtConfig) if userInput is None and isinstance(self, NumInputBox): userInput = "" if userInput is not None: if inputWidth and inputWidth < text.width: inputWidth = text.width self["Input"] = TextInputCanvas(inputWidth, userInput, "Click to enter your response", **txtConfig).config(anchor=TOP, bg="white") self["Input"].ti.config(blurAction=False, mb=self).bind(onaction=_tiAction) # Position controls and add title bar self._arrange().config(bg="#f0f0f0", weight=2, border="blue") if title: self.title(title, **txtConfig)
def setup(sk): # Load image originals fldr = resolvePath("img", __file__) sk.imgs = [Image("{}/{}.png".format(fldr, f)) for f in ("target", "paper", "scissors")] # Create button controls and add images to buttons w, h = sk.size x = w / 4 - 44 for img in sk.imgs: btn = Button((40, 40), 2).config(anchor=BOTTOM, pos=(x, h-4)) btn += Image(img).config(pos=btn.center, height=32) sk += btn.bind(onclick) # Bind event handlers x += 44 # Initialize scores to zero font = {"font":Font.mono(), "fontSize":40, "fontStyle":BOLD} sk["Score1"] = Text(0).config(color="red", anchor=TOPLEFT, pos=(4, 4), **font) sk["Score2"] = Text(0).config(color="blue", anchor=TOPRIGHT, pos=(w-4, 4), **font) # Create status text sk["Status"] = Text("Make a choice!").config(pos=(0.75*w, h-24), font=Font.sans(), fontSize=32, color="red")
def __init__(self, size, buttons=(("Okay", True), ("Cancel", False)), text={}, **kwargs): py = p = self.padding super().__init__((size, 128)) self.config(vectors=None, **kwargs) if self.drawTitle: self.title("Vector Diagram") # Text options txtConfig = {"font":Font.sans(), "fontSize":15} txtConfig.update(text) # Text inputs w = self.width - 2 * p try: y = py + self[0].height except: y = py ti = TextInputCanvas(w, "", "Vector Expression", **txtConfig) self["Vectors"] = ti.config(anchor=TOPLEFT, pos=(p, y), weight=1).bind(onaction=self.parse) y += py + ti.height self["Grid"] = TextInputCanvas(w // 2 - p, "", "Grid Settings", **txtConfig).config(anchor=TOPLEFT, pos=(p, y), weight=1) self["Step"] = TextInputCanvas(w // 4 - p // 2, "", "Step", **txtConfig).config(anchor=TOPLEFT, pos=(p + w // 2, y), weight=1) self["Size"] = TextInputCanvas(w // 4 - p // 2, "", "Width", **txtConfig).config(anchor=TOPLEFT, pos=(3 * (2 * p + w) // 4, y), weight=1) for gr in self[-3:]: gr.bind(onaction=nothing) # Options y += py + self[-1].height text = "Resultant", "Components", "Draggable" opt = Options(text, txtConfig=txtConfig).config(pos=(p, y), anchor=TOPLEFT) self["Options"] = opt x = (self.width - opt.width - p - w) / 2 x = self.width - x - w / 2 y += p y0 = y + opt.height opt[0].selected = True for gr in opt[:3]: gr.bind(onaction=nothing) # Buttons w = self.buttonWidth for btn in buttons: name = btn if type(btn) is str else btn[0] gr = Button((w, 32), 2).textIcon(*btn).setCanvas(self, name).config(anchor=TOPRIGHT, pos=(self.width - p, y)) #, pos=(x - 64, y)) y += 32 + py gr[-1].config(color="blue", align=CENTER, **txtConfig) # Resize self._size = size, max(y, y0)
def __init__(self, text, height=None, padding=4, imgs=None, txtConfig={}): text = [Text(t).config(**txtConfig) for t in text] check = [] w = 0 y = padding if not height: height = 1 + text[0].height // len(text[0].data.split("\n")) for t in text: cb = Button.checkbox(imgs).config(height=height) yc = y + t.height / 2 check.append(cb.config(height=height, pos=(padding, yc), anchor=LEFT)) t.config(pos=(cb.width + 2 * padding, yc), anchor=LEFT, **txtConfig) y += t.height + padding w1 = cb.width + t.width if w1 > w: w = w1 super().__init__((w + 3 * padding, y)) self += check + text self.boxes = check if hasattr(self, "selected"): self.selected = 0
def __init__(self, text, height=None, padding=4, imgs=None, txtConfig={}): text = [Text(t).config(**txtConfig) for t in text] check = [] w = 0 y = padding if not height: height = 1 + text[0].height // len(text[0].data.split("\n")) for t in text: cb = Button.checkbox(imgs).config(height=height) yc = y + t.height / 2 check.append( cb.config(height=height, pos=(padding, yc), anchor=LEFT)) t.config(pos=(cb.width + 2 * padding, yc), anchor=LEFT, **txtConfig) y += t.height + padding w1 = cb.width + t.width if w1 > w: w = w1 super().__init__((w + 3 * padding, y)) self += check + text self.boxes = check if hasattr(self, "selected"): self.selected = 0
def drawHighScores(self, score): "Draw the Top 10 scores on the screen" w, h = self.size x = w / 6 y = h / 5 attr = dict(color="blue", font=FONT, fontSize=self.height / 15) for i in range(len(score)): if i == 5: x += 0.5 * w y = h / 5 s = score[i] s, name = s if len(name) > 12: name = name[:12] self += Text(data=s).config(anchor=RIGHT, pos=(x - 20, y), **attr) self += Text(data=name).config(anchor=LEFT, pos=(x, y), **attr) y += self[-1].height + 8 icon = Sprite(Asteroid.original).config(spin=0.4) okay = Text("Okay").config(font=FONT) self += Button((w / 7, h / 10)).bind(onclick=restart).textIcon( okay, icon, 10).config(pos=(self.center[0], 0.9 * h), anchor=BOTTOM, border="blue", weight=3)
def __init__(self, sk): # Radio buttons text = self.options attr = {"font":sk.font, "fontSize":16} radio = [Radio(text, txtConfig=attr).config(anchor=TOPLEFT), Radio(text[1:], txtConfig=attr).config(anchor=TOPLEFT)] # Play button icon = Sprite(SoccerBall.ballImage).config(spin=1) play = Text("Play").config(**attr) play = Button((96,36), 2).textIcon(play, icon) x, y = icon.pos x += icon.width / 2 icon.config(anchor=CENTER, pos=(x,y)) # Titles attr.update(anchor=TOP) text = [Text(t + " Robot").config(color=t, **attr) for t in ("Red", "Yellow")] # Create canvas items = radio + text + [play] w = max(gr.width for gr in items) + 16 h = sum(gr.height for gr in items) + 72 super().__init__((w, h), "#d0d0d0") # Position controls xc = self.center[0] y = 8 for gr in [text[0], radio[0], text[1], radio[1]]: x = xc if isinstance(gr, Text) else 8 self += gr.config(pos=(x,y)) y += gr.height + 8 if gr is radio[0]: y += 16 self += play.config(pos=(xc,h-8), anchor=BOTTOM)
def __init__(self, text, height=None, padding=4, imgs=None, txtConfig={}): if imgs is None: imgs = Button._radioTiles() super().__init__(text, height, padding, imgs, txtConfig)