def test_tabs_with_controls_add(): t = Tabs(tabs=[ Tab(text="Tab1", controls=[Button( text="OK"), Button(text="Cancel")]), Tab( "Tab2", controls=[Textbox(label="Textbox 1"), Textbox(label="Textbox 2")], ), ]) assert isinstance(t, pglet.Control) assert isinstance(t, pglet.Tabs) assert t.get_cmd_str() == [ Command(indent=0, name=None, values=["tabs"], attrs={"value": "Tab1"}, lines=[], commands=[]), Command(indent=2, name=None, values=["tab"], attrs={"text": "Tab1"}, lines=[], commands=[]), Command(indent=4, name=None, values=["button"], attrs={"text": "OK"}, lines=[], commands=[]), Command(indent=4, name=None, values=["button"], attrs={"text": "Cancel"}, lines=[], commands=[]), Command(indent=2, name=None, values=["tab"], attrs={"text": "Tab2"}, lines=[], commands=[]), Command(indent=4, name=None, values=["textbox"], attrs={"label": "Textbox 1"}, lines=[], commands=[]), Command(indent=4, name=None, values=["textbox"], attrs={"label": "Textbox 2"}, lines=[], commands=[]), ], "Test failed"
def basic_textboxes(): return Stack(controls=[ Stack(gap=25, horizontal=True, controls=[ Textbox(label='Standard'), Textbox(label='Disabled', disabled=True) ]), Stack(gap=25, horizontal=True, controls=[ Textbox(label='Read-only', read_only=True), Textbox(label="With placeholder", placeholder='Please enter text here') ]), Stack(gap=25, horizontal=True, controls=[ Stack(controls=[ Textbox(label='Required:', required=True), Textbox(required=True) ]), Textbox(label="With error message", error_message='Error message') ]), Stack( gap=25, horizontal=True, controls=[ Textbox(label='With an icon', icon='Emoji2'), #need icon property Textbox(label='Password with reveal button', password=True) ]), Stack(gap=25, horizontal=True, controls=[textbox_with_onchange()]) ])
def underlined_borderless_textboxes(): return Stack(controls=[ Stack(gap=25, controls=[ Textbox(label='Underlined', underlined=True, placeholder='Enter text here'), Textbox(label='Borderless', borderless=True, placeholder='Enter text here') ]) ])
def test_stack_add(): s = Stack(controls=[ Textbox(id="firstName"), Textbox(id="lastName") ]) assert isinstance(s, pglet.Control) assert isinstance(s, pglet.Stack) #raise Exception(s.get_cmd_str()) assert s.get_cmd_str() == ( 'stack\n' ' textbox id="firstName"\n' ' textbox id="lastName"' ), "Test failed"
def suffix_prefix_textboxes(): return Stack(controls=[ Stack(gap=25, horizontal=True, controls=[ Textbox(label='With prefix', prefix='https://'), Textbox(label='With suffix', suffix='.com') ]), Stack(horizontal=True, controls=[ Textbox(label='With prefix and suffix', prefix='https://', suffix='.com') ]) ])
def main(page): page.update(Page(title="Counter")) page.clean() def on_click(e): try: count = int(page.get_value('number')) #if we get here the number is int page.send('set number errorMessage=""') if e.data == '+': page.set_value('number', count + 1) elif e.data == '-': page.set_value('number', count - 1) except ValueError: page.send('set number errorMessage="Please enter a number"') page.add( Stack(horizontal=True, controls=[ Button(text='-', onclick=on_click, data='-'), Textbox(id='number', value='0', align='right'), Button(text='+', onclick=on_click, data='+'), ]))
def test_add_controls_to_another_control(page): stack = Stack(id="stack1", horizontal=True) page.add(stack) t1 = page.add(Textbox(id="firstName", label="First name:"), to=stack, at=0) assert t1.id == "stack1:firstName", "Test failed"
def __init__(self, app, name): self.app = app self.display_task = Checkbox(value=False, label=name, on_change=self.status_changed) self.edit_name = Textbox(width='100%') self.display_view = Stack( horizontal=True, horizontal_align='space-between', vertical_align='center', controls=[ self.display_task, Stack(horizontal=True, gap='0', controls=[ Button(icon='Edit', title='Edit todo', on_click=self.edit_clicked), Button(icon='Delete', title='Delete todo', on_click=self.delete_clicked) ]), ]) self.edit_view = Stack(visible=False, horizontal=True, horizontal_align='space-between', vertical_align='center', controls=[ self.edit_name, Button(text='Save', on_click=self.save_clicked) ]) self.view = Stack(controls=[self.display_view, self.edit_view])
def main(page): page.title = "Counter" page.update() def on_click(e): try: count = int(txt_number.value) txt_number.error_message = "" if e.data == "+": txt_number.value = count + 1 elif e.data == "-": txt_number.value = count - 1 except ValueError: txt_number.error_message = "Please enter a number" page.update() txt_number = Textbox(value="0", align="right") page.add( Stack( horizontal=True, controls=[ Button("-", on_click=on_click, data="-"), txt_number, Button("+", on_click=on_click, data="+"), ], ))
def test_nested_stacks_update(): stack = Stack(controls=[ Textbox(id="firstName"), Textbox(id="lastName"), Stack(horizontal=True, controls=[ Button(id="ok", text="OK"), Button(id="cancel", text="Cancel") ]) ]) # open page p = pglet.page(no_window=True) ctrls = p.add(stack) assert ['_1', 'firstName', 'lastName', '_2', 'ok', 'cancel'] == [ctrls[0].id, ctrls[1].id, ctrls[2].id, ctrls[3].id, ctrls[4].id, ctrls[5].id], "Test failed" # empty update assert stack.get_cmd_str(update=True) == "", "Test failed" # update stack element ctrls[0].horizontal=True assert stack.get_cmd_str(update=True) == '"_1" horizontal="true"', "Test failed" # update inner elements ctrls[1].value = "John" ctrls[2].value = "Smith" ctrls[4].primary = True #raise Exception(stack.get_cmd_str(update=True)) assert stack.get_cmd_str(update=True) == ( '"firstName" value="John"\n' '"lastName" value="Smith"\n' '"ok" primary="true"' ), "Test failed" # assert stack.get_cmd_str(update=True) == ( # 'stack\n' # ' textbox id="firstName"\n' # ' textbox id="lastName"\n' # ' stack horizontal="true"\n' # ' button id="ok" text="OK"\n' # ' button id="cancel" text="Cancel"' # ), "Test failed"
def multiline_textboxes(): return Stack(controls=[ Stack(gap=25, horizontal=True, controls=[ Textbox(label='standard', multiline=True), Textbox(label='disabled', multiline=True, disabled=True) ]), Stack( gap=25, horizontal=True, controls=[ Textbox(label='With auto adjusted height', multiline=True, auto_adjust_height=True ) #need auto-adjusted height property ]) ])
def main(page): def btn_click(e): name = txt_name.value page.clean(True) page.add(Text(f"Hello, {name}!")) txt_name = Textbox("Your name") page.add(txt_name, Button("Say hello!", on_click=btn_click))
def textbox_with_onchange(): def textbox_changed(e): displayed_text.value = entered_text.value stack.update() entered_text = Textbox(label='With onchange event', on_change=textbox_changed) displayed_text = Text() stack = Stack(controls=[entered_text, displayed_text]) return stack
def test_nested_stacks_add(): s = Stack(controls=[ Textbox(id="firstName"), Textbox(id="lastName"), Stack(horizontal=True, controls=[ Button(id="ok", text="OK", primary=True), Button(id="cancel", text="Cancel") ]) ]) assert isinstance(s, pglet.Control) assert isinstance(s, pglet.Stack) #raise Exception(s.get_cmd_str()) assert s.get_cmd_str() == ( 'stack\n' ' textbox id="firstName"\n' ' textbox id="lastName"\n' ' stack horizontal="true"\n' ' button id="ok" primary="true" text="OK"\n' ' button id="cancel" text="Cancel"' ), "Test failed"
def change_items_in_choicegroup(): def add_clicked(e): cg.options.append(choicegroup.Option(new_option.value)) new_option.value = '' stack.update() cg = ChoiceGroup() new_option = Textbox(placeholder='Enter new item name') add = Button("Add", on_click=add_clicked) stack = Stack( controls=[cg, Stack(horizontal=True, controls=[new_option, add])]) return stack
def test_tabs_with_controls_add(): t = Tabs(tabs=[ Tab(text='Tab1', controls=[ Button(text='OK'), Button(text='Cancel') ]), Tab('Tab2', controls=[ Textbox(label='Textbox 1'), Textbox(label='Textbox 2') ]) ]) assert isinstance(t, pglet.Control) assert isinstance(t, pglet.Tabs) assert t.get_cmd_str() == ( 'tabs\n' ' tab text="Tab1"\n' ' button text="OK"\n' ' button text="Cancel"\n' ' tab text="Tab2"\n' ' textbox label="Textbox 1"\n' ' textbox label="Textbox 2"' ), "Test failed"
def change_items_in_dropdown(): def add_clicked(e): d.options.append(dropdown.Option(new_option.value)) d.value = new_option.value new_option.value = '' stack.update() d = Dropdown() new_option = Textbox(placeholder='Enter new item name') add = Button("Add", on_click=add_clicked) stack = Stack( controls=[d, Stack(horizontal=True, controls=[new_option, add])]) return stack
def test_nested_stacks_add(): s = Stack( controls=[ Textbox(id="firstName"), Textbox(id="lastName"), Stack( horizontal=True, controls=[ Button(id="ok", text="OK", primary=True), Button(id="cancel", text="Cancel"), ], ), ] ) assert isinstance(s, pglet.Control) assert isinstance(s, pglet.Stack) # raise Exception(s.get_cmd_str()) assert s.get_cmd_str() == [ Command(indent=0, name=None, values=["stack"], attrs={}, lines=[], commands=[]), Command(indent=2, name=None, values=["textbox"], attrs={"id": ("firstName", True)}, lines=[], commands=[]), Command(indent=2, name=None, values=["textbox"], attrs={"id": ("lastName", True)}, lines=[], commands=[]), Command(indent=2, name=None, values=["stack"], attrs={"horizontal": "true"}, lines=[], commands=[]), Command( indent=4, name=None, values=["button"], attrs={"primary": "true", "text": "OK", "id": ("ok", True)}, lines=[], commands=[], ), Command( indent=4, name=None, values=["button"], attrs={"text": "Cancel", "id": ("cancel", True)}, lines=[], commands=[], ), ], "Test failed"
def test_stack_add(): s = Stack( horizontal=True, vertical_fill=True, horizontal_align="center", vertical_align="baseline", gap="large", wrap=True, scroll_x=True, scroll_y=True, controls=[Textbox(id="firstName"), Textbox(id="lastName")], ) assert isinstance(s, pglet.Control) assert isinstance(s, pglet.Stack) # raise Exception(s.get_cmd_str()) assert s.get_cmd_str() == [ Command( indent=0, name=None, values=["stack"], attrs={ "gap": "large", "horizontal": "true", "horizontalalign": "center", "scrollx": "true", "scrolly": "true", "verticalalign": "baseline", "verticalfill": "true", "wrap": "true", }, lines=[], commands=[], ), Command(indent=2, name=None, values=["textbox"], attrs={"id": ("firstName", True)}, lines=[], commands=[]), Command(indent=2, name=None, values=["textbox"], attrs={"id": ("lastName", True)}, lines=[], commands=[]), ], "Test failed"
def test_add_textbox(): # open page p = pglet.page('test_textbox', no_window=True) tb_value = "Line1\nLine2\nLine3" # add textbox txt1 = Textbox(value=tb_value, multiline=True) tb = p.add(txt1) assert tb.id.startswith('_'), "Test failed" assert tb.id == txt1.id, "Test failed" # get textbox value ret_tb_value = p.get_value(tb) assert ret_tb_value == tb_value, "Test failed"
def __init__(self): self.new_task = Textbox(placeholder='Whats needs to be done?', width='100%') self.tasks_view = Stack() # application's root control (i.e. "view") containing all other controls self.view = Stack(width='70%', controls=[ Stack(horizontal=True, on_submit=self.add_clicked, controls=[ self.new_task, Button('Add', on_click=self.add_clicked) ]), self.tasks_view ])
def __init__(self): self.tasks = [] self.new_task = Textbox(placeholder="Whats needs to be done?", width="100%") self.tasks_view = Stack() self.filter = Tabs( value="all", on_change=self.tabs_changed, tabs=[Tab(text="all"), Tab(text="active"), Tab(text="completed")], ) self.items_left = Text("0 items left") self.view = Stack( width="70%", controls=[ Text(value="Todos", size="large", align="center"), Stack( horizontal=True, on_submit=self.add_clicked, controls=[ self.new_task, Button(primary=True, text="Add", on_click=self.add_clicked), ], ), Stack( gap=25, controls=[ self.filter, self.tasks_view, Stack( horizontal=True, horizontal_align="space-between", vertical_align="center", controls=[ self.items_left, Button( text="Clear completed", on_click=self.clear_clicked ), ], ), ], ), ], )
def __init__(self): self.tasks = [] self.new_task = Textbox(placeholder='Whats needs to be done?', width='100%') self.tasks_view = Stack() self.filter = Tabs(value='all', on_change=self.tabs_changed, tabs=[ Tab(text='all'), Tab(text='active'), Tab(text='completed')]) self.view = Stack(width='70%', controls=[ Text(value='Todos', size='large', align='center'), Stack(horizontal=True, on_submit=self.add_clicked, controls=[ self.new_task, Button(primary=True, text='Add', on_click=self.add_clicked)]), Stack(gap=25, controls=[ self.filter, self.tasks_view ]) ])
def main(page): # now we want the button to do something # we add a new event def on_click(e): # we get the entered text name = page.get_value(txt_name) # then cleaning the page page.clean() # and adding the text with our greeting page.add(Text(f"Hello, {name}!")) # on the page we need a textbox # and a button txt_name = Textbox("Your name") btn_hello = Button("Say hello!", onclick=on_click) # then we add controls to the page page.add(txt_name, btn_hello)
def __init__(self, app, name): self.app = app self.display_task = Checkbox( value=False, label=name, on_change=self.status_changed ) self.edit_name = Textbox(width="100%") self.display_view = Stack( horizontal=True, horizontal_align="space-between", vertical_align="center", controls=[ self.display_task, Stack( horizontal=True, gap="0", controls=[ Button( icon="Edit", title="Edit todo", on_click=self.edit_clicked ), Button( icon="Delete", title="Delete todo", on_click=self.delete_clicked, ), ], ), ], ) self.edit_view = Stack( visible=False, horizontal=True, horizontal_align="space-between", vertical_align="center", controls=[self.edit_name, Button(text="Save", on_click=self.save_clicked)], ) self.view = Stack(controls=[self.display_view, self.edit_view])
import pglet from pglet import Page, Text, Button, Stack, Textbox page = pglet.page("index") page.update(Page(title="Greeter")) page.clean() def on_click(e): name = page.get_value('name') page.add(Text(value='Hello, ' + name)) page.add( Stack(controls=[Text(value='Please enter your name:'), Textbox(id='name')]), Stack(horizontal=True, controls=[ Button(text='OK', onclick=on_click, data='OK'), ])) page.wait_close()
def test_textbox_update(): tb = Textbox(id="txt1", error_message="Enter the value") assert tb.get_cmd_str( update=True) == '"txt1" errorMessage="Enter the value"', "Test failed"
def tabs(page): message = Message(dismiss=True, visible=False) def tabs_changed(e): message.visible = True message.value = f'Tabs changed to "{e.control.value}", count increased' e.control.tabs[2].count += 1 page.update() link_tabs = Tabs(margin=10, on_change=tabs_changed, tabs=[ Tab(text='Regular tab', controls=[ Stack(horizontal=True, controls=[ Text('This is tab1'), Text('This is tab1 - line2') ]) ]), Tab(icon='Globe', text='Tab with icon', controls=[ Stack(gap=10, controls=[ Text(value='This is tab2'), Text(value='This is tab2 - line2') ]) ]), Tab(text='Tab with icon and count', icon='Ringer', count=0, controls=[ Stack(gap=10, controls=[ Text('This is tab3'), Text('This is tab3 - line2') ]) ]) ]) solid_tabs = Tabs(solid=True, margin='10px', tabs=[ Tab(text='JavaScript', icon='Code', count=10, controls=[Textbox(label='Some textbox')]), Tab(text='C#', count=30, controls=[Button(text='Hello button!')]), Tab(text='Python', count=0, controls=[Text(value='Just text...')]) ]) return Stack( gap=30, width='50%', controls=[ message, Stack(controls=[ Text("Link tabs with Change event", size="xLarge"), link_tabs ]), Stack(controls=[Text("Solid tabs", size="xLarge"), solid_tabs]) ])
load_products('C:/Projects/Python/pglet_samples/products.txt') load_units() page = pglet.page("index") page.update(Page(title="Weight Converter")) page.clean() page.add( Stack(controls=[ Stack(horizontal = True, controls=[ Text(value='Product name: '), Textbox(id='product_name', align = 'right'), Text(value='Grams in a cup: '), Textbox(id='grams_in_a_cup', align = 'right'), Button(text='Add', onclick=add_product, data='Add'), Button(text='Edit', onclick=edit_product, data='Edit'), Button(text='Delete', onclick=delete_product, data='Delete') ]), Text(id='product_prompt', value='Product prompt') ]) ) page.add(Dropdown(id='product', label = 'Select product:', options = names, value = 'Water')) page.add( Stack(horizontal = True, controls=[ Text(value='Original quantity: '),
def test_add_controls_list(page): t1 = Textbox(id="firstName", label="First name:") t2 = Textbox(id="lastName", label="Last name:") result = page.add([t1, t2], to="page", at=0) assert result == [t1, t2], "Test failed"