def test_construction(self): gui = GUI() GUI(Text("left"), Text("hi")) with self.assertRaises(TypeError): gui = GUI(0)
def test_stop_running__raises_if_not_running(self): gui = GUI() with self.assertRaises(RuntimeError): gui.stop_running() # Also make sure it raises if the GUI ran in the past, but then stopped. with self.running_in_background(gui): pass with self.assertRaises(RuntimeError): gui.stop_running()
def test_event_dispatch(self): decoy1 = Button() button = Button() decoy2 = Button() xs = [] button.callback = (lambda: xs.append(1)) gui = GUI(decoy1, button, decoy2) gui.dispatch_event(Click(target_id=button.id)) self.assertEqual([1], xs)
def test_callbacks_produce_commands(self): button_with_predefined_callback = Button(callback=(lambda: None)) button_with_later_callback = Button() gui = GUI(button_with_predefined_callback, button_with_later_callback) stream = gui.command_stream() self.assertIn(button_with_predefined_callback.id, stream.get(block=False)) self.assertTrue(stream.empty()) button_with_later_callback.set_callback(lambda: None) self.assertIn(button_with_later_callback.id, stream.get(block=False)) self.assertTrue(stream.empty())
def test_event_dispatch(self): decoy1 = Button() button = Button() decoy2 = Button() xs = [] button.set_callback(lambda: xs.append(1)) gui = GUI(decoy1, button, decoy2) event = {'type': CLICK, 'id': button.id} gui.dispatch_event({'type': CLICK, 'id': button.id}) self.assertEqual([1], xs)
def main(): gui = GUI(Paragraph(""" Run commands in the REPL. As you change `gui`, this page will update. Some commands you might run are: """)) for sample in ("gui.body.append(Text('Hiiii!'))", "gui.body.append(Button(callback=(lambda: gui.body.append(Paragraph('Clicked!')))))"): gui.body.append(CodeBlock(sample)) t = threading.Thread(target=gui.run, kwargs={'quiet': True}) t.daemon = True t.start() run_repl(gui) if gui.running: # If the user killed the GUI in the REPL, it might not still be running. gui.stop_running()
def main(): from browsergui import GUI def cb(): evens = [x for x in data if x % 2 == 0] print('Evens:', evens) data = range(10) b = ToggleableButton(name='print ', callback=cb) gui = GUI(b.button).run()
def test_run(self): # Just make sure that modifications before/during/after runs don't blow up, # and that stop_running() terminates the run()-thread. gui = GUI(Text('before first run')) with self.running_in_background(gui): gui.body.append(Text('during first run')) gui.body.append(Text('before second fun')) with self.running_in_background(gui): gui.body.append(Text('during second run'))
def main(): now = Text("") def update_now_forever(): while True: now.text = time.strftime("%Y-%m-%d %H:%M:%S") time.sleep(1) t = threading.Thread(target=update_now_forever) t.daemon = True t.start() GUI(Text("The time is: "), now).run()
def test_command_stream(self): gui = GUI() stream = gui.command_stream() while not stream.empty(): stream.get(block=False) gui.send_command("foo") self.assertEqual("foo", stream.get(block=False)) self.assertTrue(stream.empty()) stream2 = gui.command_stream() while not stream2.empty(): stream2.get(block=False) gui.send_command("bar") self.assertEqual("bar", stream.get(block=False)) self.assertTrue(stream.empty()) self.assertEqual("bar", stream2.get(block=False)) self.assertTrue(stream2.empty())
def test_run__raises_if_running(self): gui = GUI() with self.running_in_background(gui): with self.assertRaises(RuntimeError): self.run_quietly(gui)
import code import threading import browsergui from browsergui import GUI, Paragraph, CodeBlock, Paragraph, run, call_in_background gui = GUI(Paragraph(""" Run commands in the REPL. As you change `gui`, this page will update. Some commands you might run are: """)) for sample in ("gui.append(Text('Hiiii!'))", "gui.append(Button(callback=(lambda: gui.append(Paragraph('Clicked!')))))"): gui.append(CodeBlock(sample)) gui.append(Paragraph("The code for this page is:")) gui.append(CodeBlock(open(__file__).read())) def run_repl(): interpreter = code.InteractiveConsole(locals={'_gui': gui}) interpreter.runsource('from browsergui import *') interpreter.runsource('gui = _gui') interpreter.interact( banner=""" Here's an interpreter! You have access to everything in the `browsergui` namespace, plus a ready-made GUI named `gui`. The server startup might print a couple things on top of the prompt - don't worry, you're still in the interpreter. Exiting the interpreter will terminate the program.
from browsergui import GUI, Button, Text, CodeSnippet, Container, run gui = GUI(Text("What follows is the program that generates this page.")) for line in open(__file__).readlines(): text = CodeSnippet(" "+line.strip("\n")) button = Button("Toggle line", callback=text.toggle_visibility) gui.append(Container(button, text)) def main(): run(gui) if __name__ == '__main__': main()
def main(): GUI(Text("Hello world!")).run()