def test_getGridElement(self): grid = Grid(App()) self.assertEqual(None, grid.getGridElement(Vec2(1, 0))) label = Label(grid, "hello", column=1, row=0) self.assertEqual(label, grid.getGridElement(Vec2(1, 0))) label = Label(grid, column=0, row=0) self.assertEqual(label, grid.getGridElement(Vec2(0, 0)))
def test_nextSibling(self): page = Page(App()) button = Button(page, "button") label = Label(page, "label") checkbutton = Checkbutton(page) self.assertEqual(button.nextSibling(), label) self.assertEqual(label.nextSibling(), checkbutton) self.assertEqual(checkbutton.nextSibling(), button) self.assertEqual(checkbutton.previousSibling(), label) self.assertEqual(button.previousSibling(), checkbutton)
def test_append(self): grid = Grid(App()) # Column 2 self.assertEqual(Vec2(2, 0), grid.appendToColumn(Label(grid, pack=False), 2)) self.assertEqual(Vec2(2, 1), grid.appendToColumn(Label(grid, pack=False), 2)) # Column 1 self.assertEqual(Vec2(1, 0), grid.appendToColumn(Label(grid, pack=False), 1)) # Row 0 self.assertEqual(Vec2(0, 0), grid.appendToRow(Label(grid, pack=False), 0)) self.assertEqual(Vec2(3, 0), grid.appendToRow(Label(grid, pack=False), 0)) self.assertEqual(Vec2(4, 0), grid.appendToRow(Label(grid, pack=False), 0)) # Row 1 self.assertEqual(Vec2(0, 1), grid.appendToRow(Label(grid, pack=False), 1)) self.assertEqual(Vec2(1, 1), grid.appendToRow(Label(grid, pack=False), 1)) # Column 0 self.assertEqual(Vec2(0, 2), grid.appendToColumn(Label(grid, pack=False), 0))
def test_toggleShow(self): app = App() label = Label(Page(app), "testing") self.assertEqual(True, label.isPacked()) label.toggleShow(mainloop=False) self.assertEqual(False, label.isPacked()) label.toggleShow(mainloop=False) self.assertEqual(True, label.isPacked())
def test_config(self): app = App() label = Label(Page(app), "testing") self.assertEqual(True, "bg" in label.getAllWidgetConfigs()) self.assertEqual("SystemButtonFace", label.getWidgetConfig("bg")) label.widgetConfig(bg="red") self.assertEqual("red", label.getWidgetConfig("bg")) with self.assertRaises(tk.TclError): label.getWidgetConfig("doesntexist")
def test_packPart(self): elementList = ElementList(Page(App()), maxFirstSteps=5) for i in range(6): Label(elementList, "hello") self.assertEqual([ Vec2(0, 0), Vec2(0, 1), Vec2(0, 2), Vec2(0, 3), Vec2(0, 4), Vec2(1, 0) ], [elementList.getGridPos(ele) for ele in elementList.getChildren()]) elementList.removeChildren() elementList.maxFirstSteps = 2 for i in range(6): LabelEntry(elementList, "hello") self.assertEqual([ Vec2(0, 0), Vec2(0, 1), Vec2(1, 0), Vec2(1, 1), Vec2(2, 0), Vec2(2, 1) ], [elementList.getGridPos(ele) for ele in elementList.getChildren()])
def test_place(self): app = App() label = Label(Page(app, width=500, height=500), "testing", pack=False) label.place(Vec2(100, 100)) label.show(mainloop=False) self.assertEqual(Vec2(100, 100), label.getTopLeftPos())
def test_parents(self): app = App() for page in Page(app), Page(app, width=200, height=200): Label(page, "hello") Label(page, "there") page2 = Page(page) text3 = Label(page2, "in page 2") self.assertIs(text3.getTopPage(), page) self.assertEqual(text3.getParents(), [page2, page]) self.assertEqual(text3.getParents(includeSelf=True), [text3, page2, page]) self.assertEqual([page2.frame, page2, page.frame, page], text3.getParents(includeParts=True)) page3 = Page(page2, scrollable=True) text4 = Label(page3, "in page 3") self.assertEqual([page2.frame, page2, page.frame, page], page3.getParents(includeParts=True)) self.assertEqual([page3.canvasFrame, page3.canvas, page3.frame, page3, page2.frame, page2, page.frame, page], text4.getParents(includeParts=True))
def test_label(self): app = App() for page in Page(app), Page(app, width=200, height=200): label = Label(page) self.assertEqual("", label.getValue()) label = Label(page, "hello") self.assertEqual(label.parentPage, page) self.assertIs(label.widget.element, label) self.assertFalse(label.isShown()) label.show(mainloop=False) self.assertTrue(label.isShown()) page.remove() self.assertRaises(tk.TclError, label.isShown)
def test__clickNextButton(self): page = Page(App()) entry = Entry(page, "hello") Label(page, "random") Button(page, "Change default", lambda: 5) self.assertEqual([5], entry._clickNextButton()) entry2 = Entry(page, "hello") Button(page, "Change default", lambda: 3) self.assertEqual([3], entry2._clickNextButton()) page.app.remove()
def test_getGridSize(self): grid = Grid(App()) self.assertEqual(Vec2(0, 0), grid.getGridSize()) label1 = Label(grid, "hello", column=1, row=0) self.assertEqual(Vec2(2, 1), grid.getGridSize()) label2 = Label(grid, "hello", column=2, row=1) self.assertEqual(Vec2(3, 2), grid.getGridSize()) label2.remove() self.assertEqual(Vec2(2, 1), grid.getGridSize()) label1.remove() self.assertEqual(Vec2(0, 0), grid.getGridSize())
def test_init(self): page = Page(App()) label = Label(page, "random") page.show(mainloop=False) red = label.createStyle("color red", bg="red", priority=3) blue = label.createStyle("color blue", bg="blue", priority=2) self.assertEqual(False, red.isEnabled()) blue.enable() self.assertEqual("blue", label.getWidgetConfig("bg")) blue.disable() red.enable() self.assertEqual(True, red.isEnabled()) self.assertEqual("red", label.getWidgetConfig("bg")) blue.enable() self.assertEqual("red", label.getWidgetConfig("bg")) red.disable() self.assertEqual(False, red.isEnabled()) self.assertEqual("blue", label.getWidgetConfig("bg")) blue.delete() self.assertEqual(False, blue.isEnabled()) self.assertEqual(red.styleHandler.originalStyle["bg"], label.getWidgetConfig("bg")) self.assertRaises(AttributeError, blue.enable) self.assertEqual(False, blue.isEnabled()) self.assertEqual(2, len(label.styleHandler.allStyles)) self.assertEqual(1, len(label.styleHandler.styles.objects)) red.enable() self.assertEqual(2, len(label.styleHandler.allStyles)) self.assertEqual(2, len(label.styleHandler.styles.objects)) red.enable() self.assertEqual(2, len(label.styleHandler.allStyles)) self.assertEqual(2, len(label.styleHandler.styles.objects)) page.app.remove()
def __init__(self, parentPage=None, value=None, default=None, width=15, **parameters): super().__init__(parentPage=parentPage, pady=4, **parameters) self.label = Label(self, value=value, side="left", padx=4) self.entry = Entry(self, default=default, width=width, side="left", padx=4) self.pack()
def test_getGridPos(self): grid = Grid(App()) label = Label(Grid(App()), "hello") self.assertRaises(AttributeError, grid.getGridPos, label) label = Label(grid, "hello", column=1, row=1, pack=False) self.assertRaises(AttributeError, grid.getGridPos, label) label.pack() self.assertEqual(Vec2(1, 1), grid.getGridPos(label))
def test_getFirstEmptyPos(self): grid = Grid(App()) # Simple column with empty grid self.assertEqual(Vec2(1, 0), grid.getFirstEmptyPos(Vec2(1, 0), Vec2(0, 1))) self.assertEqual(Vec2(1, 0), grid.getFirstEmptyPos(Vec2(1, -1), Vec2(0, -1))) self.assertEqual(Vec2(1, 0), grid.getFirstEmptyPos(Vec2(1, 0))) self.assertEqual(Vec2(1, 0), grid.getFirstEmptyPos(Vec2(1, -1))) Label(grid, column=1, row=1) # From top of column going down self.assertEqual(Vec2(1, 0), grid.getFirstEmptyPos(Vec2(1, 0), Vec2(0, 1))) # From bottom of column going up, over label self.assertEqual(Vec2(1, 0), grid.getFirstEmptyPos(Vec2(1, -1), Vec2(0, -1))) # From bottom of column going down without confine self.assertEqual(None, grid.getFirstEmptyPos(Vec2(1, 1), Vec2(0, 1))) # From bottom of column going down with confine self.assertEqual( Vec2(1, 0), grid.getFirstEmptyPos(Vec2(1, 1), Vec2(0, 1), confine=True))
def test_init(self): app = App() page = Page(app) page.menu("testing", return_5=lambda: 5) page.showMenu() self.assertEqual(True, app.menuPage.getElementByValue("testing") is not None) self.assertEqual( [5], app.menuPage.getElementByValue("return 5").click(animate=False)) self.assertEqual(None, app.menuPage) page.showMenu() self.assertEqual(True, app.menuPage is not None) page.hideMenu() self.assertEqual(None, app.menuPage) label = Label(page, "label text here") label.menu("labels menu", **{"info:": lambda: 2, "btn": lambda: True}) page.showMenu() self.assertEqual(True, app.menuPage.getElementByValue("testing") is not None) self.assertEqual(None, app.menuPage.getElementByValue("labels menu")) self.assertEqual(None, app.menuPage.getElementByValue("info: 2")) self.assertEqual(None, app.menuPage.getElementByValue("btn")) self.assertEqual( [5], app.menuPage.getElementByValue("return 5").click(animate=False)) label.showMenu() self.assertEqual(True, app.menuPage.getElementByValue("testing") is not None) self.assertEqual( True, app.menuPage.getElementByValue("labels menu") is not None) self.assertEqual(True, app.menuPage.getElementByValue("info: 2") is not None) self.assertEqual( [True], app.menuPage.getElementByValue("btn").click(animate=False))
def test_states(self): app = App() label = Label(Page(app), "random", pack=False) self.assertEqual(False, label.isShown()) self.assertEqual(True, label.exists()) self.assertEqual(False, label.isPacked()) label.show(mainloop=False) self.assertEqual(True, label.isShown()) self.assertEqual(True, label.exists()) self.assertEqual(True, label.isPacked()) label.hide() self.assertEqual(False, label.isShown()) self.assertEqual(True, label.exists()) self.assertEqual(False, label.isPacked()) label.remove() self.assertEqual(False, label.isShown(error=False)) self.assertEqual(False, label.exists()) self.assertEqual(False, label.isPacked())
def test_getFirstPatternPos(self): grid = Grid(App()) # Default pattern self.assertEqual(Vec2(0, 0), pos := grid.getFirstPatternPos()) Label(grid, pos=pos) self.assertEqual(Vec2(0, 1), pos := grid.getFirstPatternPos()) Label(grid, pos=pos) self.assertEqual(Vec2(0, 2), pos := grid.getFirstPatternPos()) Label(grid, pos=pos) self.assertEqual(Vec2(0, 3), pos := grid.getFirstPatternPos()) Label(grid, pos=pos) self.assertEqual(Vec2(0, 4), pos := grid.getFirstPatternPos()) Label(grid, pos=pos) self.assertEqual(Vec2(1, 0), pos := grid.getFirstPatternPos()) Label(grid, pos=pos) self.assertEqual(Vec2(1, 1), pos := grid.getFirstPatternPos()) Label(grid, pos=pos) # One row down, two rows high self.assertEqual( Vec2(1, 2), pos := grid.getFirstPatternPos(startPos=Vec2(1, 1), maxFirstSteps=2)) Label(grid, pos=pos) self.assertEqual( Vec2(2, 1), pos := grid.getFirstPatternPos(startPos=Vec2(1, 1), maxFirstSteps=2)) Label(grid, pos=pos) self.assertEqual( Vec2(2, 2), pos := grid.getFirstPatternPos(startPos=Vec2(1, 1), maxFirstSteps=2)) Label(grid, pos=pos) self.assertEqual( Vec2(3, 1), pos := grid.getFirstPatternPos(startPos=Vec2(1, 1), maxFirstSteps=2)) Label(grid, pos=pos) # Left down self.assertEqual( Vec2(10, 10), pos := grid.getFirstPatternPos(startPos=Vec2(10, 10), firstStep=Vec2(-1, 0), secondStep=Vec2(-1, -1), maxFirstSteps=2)) Label(grid, pos=pos) self.assertEqual( Vec2(9, 10), pos := grid.getFirstPatternPos(startPos=Vec2(10, 10), firstStep=Vec2(-1, 0), secondStep=Vec2(-1, -1), maxFirstSteps=2)) Label(grid, pos=pos) self.assertEqual( Vec2(9, 9), pos := grid.getFirstPatternPos(startPos=Vec2(10, 10), firstStep=Vec2(-1, 0), secondStep=Vec2(-1, -1), maxFirstSteps=2)) Label(grid, pos=pos) self.assertEqual( Vec2(8, 9), pos := grid.getFirstPatternPos(startPos=Vec2(10, 10), firstStep=Vec2(-1, 0), secondStep=Vec2(-1, -1), maxFirstSteps=2)) Label(grid, pos=pos) # Digaonal self.assertEqual( Vec2(20, 20), pos := grid.getFirstPatternPos(startPos=Vec2(20, 20), firstStep=Vec2(10), secondStep=Vec2(1, 1), maxFirstSteps=1)) Label(grid, pos=pos) self.assertEqual( Vec2(21, 21), pos := grid.getFirstPatternPos(startPos=Vec2(20, 20), firstStep=Vec2(10), secondStep=Vec2(1, 1), maxFirstSteps=1)) Label(grid, pos=pos) self.assertEqual( Vec2(22, 22), pos := grid.getFirstPatternPos(startPos=Vec2(20, 20), firstStep=Vec2(10), secondStep=Vec2(1, 1), maxFirstSteps=1)) Label(grid, pos=pos)
"""Random testing""" from generallibrary.time import sleep from generalgui import App, Grid, Page, Button, Label, OptionMenu, Checkbutton, Entry, LabelCheckbutton, LabelEntry, Spreadsheet from generalvector import Vec2 import pandas as pd import inspect page = Page(App(), resizeable=True) label = Label(page, "hello") label.show() # Label(page, "hello\nthere", hideMultiline=True).show()
def test_init(self): page = Page(App()) label = Label(page, "Text") bind = label.createBind("<Button-1>", lambda: 5) self.assertEqual([5], label.callBind("<Button-1>")) bind.remove() self.assertEqual([], label.callBind("<Button-1>")) label.createBind("<Button-1>", lambda: 5) page.createBind("<Button-1>", lambda: 2) self.assertEqual([5, 2], label.callBind("<Button-1>")) label.setBindPropagation("<Button-1>", False) self.assertEqual([5], label.callBind("<Button-1>")) label.setBindPropagation("<Button-1>", True) self.assertEqual([5, 2], label.callBind("<Button-1>")) label.setBindPropagation("<Button-1>", False) label.createBind("<Button-1>", lambda: 5) label.createBind("<Button-1>", lambda: 2) self.assertEqual([5, 5, 2], label.callBind("<Button-1>")) label.createBind("<Button-1>", lambda: 3, add=False) self.assertEqual([3], label.callBind("<Button-1>")) label.createBind("<Button-1>", lambda: 5, name="hello") self.assertEqual([3, 5], label.callBind("<Button-1>")) label.createBind("<Button-1>", lambda: 5, name="hello") self.assertEqual([3, 5], label.callBind("<Button-1>")) label.createBind("<Button-1>", lambda: 5, name="hello", add=False) self.assertEqual([5], label.callBind("<Button-1>")) label.onClick(lambda: 5, add=False) label.onClick(lambda: 2) self.assertEqual([5, 2], label.click(animate=False)) self.assertEqual([5, 2], label.click(animate=True)) label.onRightClick(lambda: 5, add=False) label.onRightClick(lambda: 2) self.assertEqual([5, 2], label.rightClick(animate=False)) self.assertEqual([5, 2], label.rightClick(animate=True)) label.remove() self.assertEqual([], label.callBind("<Button-1>"))
def addEles(): ss(lambda x: x.loadDataFrame( pd.DataFrame([[ Label(x.mainGrid, "testing", pos=0), Label(x.mainGrid, "hi", pos=0) ], [True, False]]).T))
def __init__(self, parentPage=None, value=None, default=False, **parameters): super().__init__(parentPage=parentPage, pady=4, **parameters) self.label = Label(self, value=value, side="left", padx=4) self.checkbutton = Checkbutton(self, default=default, side="left", padx=4, expand=True) self.pack()
def test_siblings(self): app = App() for page in Page(app), Page(app, width=200, height=200): text1 = Label(page, "hi") text2 = Label(page, "hello") self.assertEqual(text1.getSiblings(), [text2]) self.assertEqual(text2.getSiblings(), [text1]) self.assertEqual(text1.getSiblings(ignore=text1), [text2]) self.assertEqual(text1.getSiblings(ignore=text2), []) self.assertEqual(text1.getSiblings(ignore=(text1, text2)), []) self.assertFalse(text1.isShown()) self.assertFalse(text2.isShown()) text1.showSiblings(mainloop=False) self.assertTrue(text2.isShown()) self.assertTrue(text1.isShown()) text1.hideSiblings(ignore=text2) self.assertTrue(text1.isShown()) self.assertTrue(text2.isShown()) text1.hideSiblings() self.assertTrue(text1.isShown()) self.assertFalse(text2.isShown()) text2.removeSiblings() self.assertRaises(tk.TclError, text1.isShown) self.assertFalse(text2.isShown()) text1.parentPage.showChildren(mainloop=False) self.assertTrue(text2.isShown())
def test_children(self): app = App() for page in Page(app), Page(app, width=200, height=200): text1 = Label(page, "hello") text2 = Label(page, "there") page2 = Page(page) text3 = Label(page2, "in page 2") # print(page.isScrollable(), page.getChildren()) self.assertEqual(page.getChildren(), [text1, text2, page2]) self.assertEqual([text1, text2, page2, text3], page.getChildren(recurrent=True)) self.assertFalse(text1.isShown()) self.assertFalse(text2.isShown()) self.assertFalse(page2.isShown()) self.assertFalse(text3.isShown()) page.show(mainloop=False) self.assertTrue(text1.isShown()) self.assertTrue(text2.isShown()) self.assertFalse(page2.isShown()) self.assertFalse(text3.isShown()) page.showChildren(mainloop=False) self.assertTrue(page2.isShown()) self.assertTrue(text3.isShown()) page.hideChildren() self.assertFalse(text1.isShown()) self.assertFalse(text2.isShown()) self.assertFalse(page2.isShown()) self.assertFalse(text3.isShown()) page.removeChildren() self.assertTrue(page.isShown()) self.assertRaises(tk.TclError, text1.isShown) self.assertRaises(tk.TclError, text2.isShown) self.assertRaises(tk.TclError, page2.isShown) self.assertRaises(tk.TclError, text3.isShown)
def test_value(self): label = Label(Page(App()), "hello") label.setValue("test") self.assertEqual("test", label.getValue()) label.setValue(None) self.assertEqual("", label.getValue()) label.setValue("") self.assertEqual("", label.getValue()) label.setValue(True) self.assertEqual(True, label.getValue())
def test_getFirstElementPos(self): grid = Grid(App()) Label(grid, column=1, row=2) # Ints sanitize self.assertRaises(Exception, grid.getFirstElementPos, Vec2(1, 0.5)) # Initial jump self.assertEqual(None, grid.getFirstElementPos(Vec2(-1, 0), Vec2(1, 1))) self.assertEqual(Vec2(1.0, 2), grid.getFirstElementPos(Vec2(1, -1))) self.assertEqual( Vec2(1, 2), grid.getFirstElementPos(Vec2(-1, 0), Vec2(1, 1), confine=True)) # Initial jump with max steps self.assertEqual( None, grid.getFirstElementPos(Vec2(-1, 0), Vec2(1, 1), confine=True, maxSteps=1)) self.assertEqual( Vec2(1, 2), grid.getFirstElementPos(Vec2(-1, 0), Vec2(1, 1), confine=True, maxSteps=2)) # No initial jump, then three jumps self.assertEqual(None, grid.getFirstElementPos(Vec2(0, 0), Vec2(1, 1))) self.assertEqual( Vec2(1, 2), grid.getFirstElementPos(Vec2(0, 0), Vec2(1, 1), confine=True)) # No jumps, move two steps self.assertEqual(Vec2(1, 2), grid.getFirstElementPos(Vec2(1, 0), Vec2(0, 1))) self.assertEqual( None, grid.getFirstElementPos(Vec2(1, 0), Vec2(0, 1), maxSteps=1)) # Two elements in row, make sure it gets first Label(grid, column=2, row=2) self.assertEqual(Vec2(1, 2), grid.getFirstElementPos(Vec2(0, 2), Vec2(1, 0))) # Step over one element self.assertEqual(Vec2(2, 2), grid.getFirstElementPos(Vec2(0, 2), Vec2(2, 0))) # Jump with multiple elements self.assertEqual(None, grid.getFirstElementPos(Vec2(0, 2), Vec2(-1, 0))) self.assertEqual( Vec2(2, 2), grid.getFirstElementPos(Vec2(0, 2), Vec2(-1, 0), confine=True)) # Initial jump with multiple elements Label(grid, column=0, row=2) self.assertEqual(Vec2(0, 2), grid.getFirstElementPos(Vec2(3, 2), Vec2(1, 0)))
def test_textOnClick(self): text = Label(Page(App()), "hello") text.onClick(lambda: 1, add=False) self.assertEqual([1], text.click()) text.onClick(lambda: 2, add=False) self.assertEqual([2], text.click()) text.onClick(lambda: 3, add=True) self.assertEqual([2, 3], text.click()) text.onClick(lambda: 4) self.assertEqual([2, 3, 4], text.click()) text.onClick(lambda: 5, add=False) self.assertEqual([5], text.click())