Last active
March 15, 2023 09:44
-
-
Save Lerking/b57103c25dbeaab8473b0312e60a2ed7 to your computer and use it in GitHub Desktop.
Python code snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Keywords: Python, gtk3, random, depth, treeview | |
| A small example on adding random depth items to a gtk.treeview | |
| To run this example, you need this file as well as the 'traverse_random_depth_nested_classes.py' | |
| """ | |
| import gi | |
| gi.require.version('Gtk', '3.0') | |
| from gi.repository import Gtk | |
| from traverse_random_depth_nested_classes import DummyStructure as DS | |
| Spoiler.... | |
| Still more to come ;-) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Small example of a nested for loop. | |
| """ | |
| LIST_1 = ["A", "B", "C", "D", "E", "F"] | |
| LIST_2 = [1, 2, 3, 4, 5, 6] | |
| def content(l1, l2): | |
| print(l1, "->", l2) | |
| [content(l1, l2) for l1 in LIST_1 for l2 in LIST_2] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Keywords: Python, random, depth, nested class | |
| Due to dataclasses, python => 3.7 is needed. | |
| If your python version is prior to 3.7 you can comment out the | |
| 'from dataclasses...' and '@dataclass' in the code | |
| """ | |
| from dataclasses import dataclass | |
| import random | |
| import string | |
| maxDummys = 5 | |
| @dataclass | |
| class DummyStructure: | |
| def __init__(self, lvl: str): | |
| self.level = lvl | |
| self.structs = [] | |
| self.name = self.random_name_generator() | |
| self.dummy_generator() | |
| @property | |
| def level(self): | |
| return self._level | |
| @level.setter | |
| def level(self, lvl): | |
| self._level = lvl | |
| @property | |
| def name(self): | |
| return self._name | |
| @name.setter | |
| def name(self, value: str): | |
| self._name = value | |
| def dummy_generator(self): | |
| if len(self.level) <= 3: | |
| for _ in range(random.randint(1,maxDummys)): | |
| lvl = self.level + str(_+1) | |
| self.structs.append(DummyStructure(lvl)) | |
| def random_name_generator(self): | |
| return ''.join(random.choice(string.ascii_letters) for _ in range(10)) | |
| if __name__ == '__main__': | |
| def print_dummies(dummys): | |
| for dummy in dummys: | |
| print(dummy.level, dummy.name) | |
| print_dummies(dummy.structs) | |
| dummys = [] | |
| for i in range(maxDummys): | |
| dummys.append(DummyStructure(str(i+1))) | |
| print_dummies(dummys) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import wx | |
| import wx.stc | |
| from wx.lib.mixins.listctrl import ListCtrlAutoWidthMixin | |
| class CheckListCtrl(wx.ListCtrl, ListCtrlAutoWidthMixin): | |
| def __init__(self, parent): | |
| wx.ListCtrl.__init__(self, parent, wx.ID_ANY, style=wx.LC_REPORT | | |
| wx.SUNKEN_BORDER) | |
| ListCtrlAutoWidthMixin.__init__(self) | |
| self.SetSize(-1, -1, -1, 50) | |
| self.EnableCheckBoxes() | |
| self.Bind(wx.EVT_LIST_ITEM_CHECKED, self.OnItemChecked) | |
| self.Bind(wx.EVT_LIST_ITEM_UNCHECKED, self.OnItemUnchecked) | |
| def OnItemChecked(self, event): | |
| item = event.GetItem() | |
| print(f'{item.GetText()} - Checked') | |
| def OnItemUnchecked(self, event): | |
| item = event.GetItem() | |
| print(f'{item.GetText()} - Unchecked') | |
| class ListViewComboPopup(wx.ComboPopup): | |
| def __init__(self): | |
| wx.ComboPopup.__init__(self) | |
| self.lc = None | |
| def AddItem(self, txt): | |
| self.lc.InsertItem(0, txt) | |
| def Init(self): | |
| self.value = -1 | |
| self.curitem = -1 | |
| def Create(self, parent): | |
| self.lc = CheckListCtrl(parent) | |
| self.lc.InsertColumn(0, '', width=90) | |
| return True | |
| def GetControl(self): | |
| return self.lc | |
| def OnPopup(self): | |
| wx.ComboPopup.OnPopup(self) | |
| def GetAdjustedSize(self, minWidth, prefHeight, maxHeight): | |
| return wx.ComboPopup.GetAdjustedSize( | |
| self, minWidth, 110, maxHeight) | |
| class MyForm(wx.Frame): | |
| def __init__(self): | |
| wx.Frame.__init__(self, None, title="Popup Menu Tutorial") | |
| panel = wx.Panel(self) | |
| comboCtrl = wx.ComboCtrl(panel, wx.ID_ANY, "Select filter") | |
| popupCtrl = ListViewComboPopup() | |
| comboCtrl.SetPopupControl(popupCtrl) | |
| popupCtrl.AddItem("Test 1") | |
| popupCtrl.AddItem("Test 2") | |
| popupCtrl.AddItem("Test 3") | |
| if __name__ == "__main__": | |
| app = wx.App(False) | |
| frame = MyForm().Show() | |
| app.MainLoop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment