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, text, userInput=None, buttons=None, title=None, size=(1,1), **kwargs): super().__init__(size) self.command = None # Collect 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 if userInput is not None: self["Input"] = TextInput(userInput, "Click to enter your response").config(anchor=TOP, bg="white", **txtConfig).bind(onaction=_tiAction) self["Text"] = Text(text).config(anchor=TOP, **txtConfig) # Position controls and add title bar self._arrange().config(bg="#f0f0f0", weight=2, border="blue") if title: self.title(title, **txtConfig)
def title(self, title, padding=4, **kwargs): "Add a title bar" txtConfig = dict(font=Font.sans(), fontSize=15, fontStyle=BOLD, color="white", padding=padding) txtConfig.update(kwargs) title = Text(title).config(**txtConfig) cv = Canvas((self.width, title.height + self.weight), self.border) cv += title.config(pos=(cv.center[0], self.weight), anchor=TOP) self.insertTop(cv, 0, "TitleBar") return self
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 click(cv, ev): m = modKeys() if m & 4: font = dict(font=Font.sans(), color="red", fontSize=18) i = 0 fn = "vector{}.png" while os.path.exists(fn.format(i)): i += 1 fn = fn.format(i) cv.save(fn).removeItems("Saved") cv["Saved"] = Text("Saved as '{}'".format(fn)).config(anchor=TOP, pos=(cv.center[0], 8), **font) elif m & 1: cv.remove() else: cv.config(layer=-1)
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 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 _font(f): "Find a requested font" found = Font.find(*f["font"].split(",")) f["font"] = found if found else Font.sans()
from sc8pr import Sketch, Canvas, Image, TOPLEFT, TOPRIGHT, CENTER, TOP from sc8pr.util import rgba, nothing, sc8prData from sc8pr.text import BOLD, Font from sc8pr.gui.textinput import TextInput from sc8pr.gui.radio import Radio, Options from sc8pr.gui.slider import Slider from sc8pr.gui.button import Button from sc8pr.gui.menu import Menu, R_TRIANGLE try: # v2.2 from sc8pr.gui.textinput import TextInputCanvas except: # v2.0-2.1 TextInputCanvas = None GREY, BLUE = rgba("#ececec", "blue") FONT = Font.sans() def setup(sk): # Create a Canvas as a GUI dialog cv = Canvas((384, 256)).config(bg="#f0f0ff", weight=1) # Vertical positioning 16 pixels below last item added down = lambda cv: 16 + cv[-1].height text = dict(color=BLUE, font=FONT, fontStyle=BOLD, padding=4) if TextInputCanvas: # v2.2.dev ti = TextInputCanvas(336, "", "Type Some Text...", **text) else: # v2.0-2.1 ti = TextInput("", "Type Some Text...").config(**text) x, y = cv.center[0] - 8, 16
# You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. "A demonstration of some of sc8pr 2.0's GUI controls" from sc8pr import Sketch, Canvas, Image, TOPLEFT, TOPRIGHT, CENTER, TOP from sc8pr.util import rgba, nothing, sc8prData from sc8pr.text import BOLD, Font from sc8pr.gui.textinput import TextInput from sc8pr.gui.radio import Radio, Options from sc8pr.gui.slider import Slider from sc8pr.gui.button import Button from sc8pr.gui.menu import Menu, R_TRIANGLE GREY, BLUE = rgba("#ececec", "blue") FONT = Font.sans() 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)