├── LICENSE ├── README.md └── packsim.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 thom-jp 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tkinter_pack_simulator 2 | This is python tkinter GUI pack method simulator 3 | 4 | # How to Use 5 | 1. Run script by python3 6 | ```bash 7 | python packsim.py 8 | ``` 9 | (for my environment, "python" indicates python3) 10 | 11 | 2. Configure items by bottom control panel 12 | 13 | ![image](https://user-images.githubusercontent.com/11180273/152720220-4496b305-90c9-4b73-a8d9-b908f8757751.png) 14 | 15 | |Control|Type|Explanation| 16 | |---|---|---| 17 | |pack|Checkbox|An item will be created on simulation window when checked it.
Every other configurations of an item are meaningless when it's unchecked.
By default, 4 items are selected.| 18 | |side|Combobox|An item will be packed on a selected side| 19 | |fill|Combobox|An item will try to be filled selected direction.
It's limited by occupied area.| 20 | |expand|Checkbox|An item will try to expand occupied area.| 21 | 22 | That's all. 23 | It'll be nice if it helps someone confusing about "pack" layout method of tkinter. 24 | 25 | # Note 26 | Packing order is always start from youngest number of items regardless your configuration operation. 27 | When you change something, actually all items are once deleted and re-packed again. 28 | 29 | # About what I didn't implement 30 | Sorry I was lazy to implement configuration reset feature. 31 | I knwo that reset feature wll be quite helpful but I think this is not an application used repeatedly after user understand how pack works. 32 | So, instead of taking effort on this, I'd like to focus on something more effective. 33 | -------------------------------------------------------------------------------- /packsim.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | from tkinter import ttk 3 | 4 | class Application(tk.Frame): 5 | def __init__(self, master=None): 6 | super().__init__(master) 7 | master.geometry("800x600") 8 | master.title("tkinter pack layout simulator") 9 | self.item_colors = ['IndianRed1', 'SteelBlue1', 'SpringGreen1', 'Goldenrod1', 'LightPink1','LightSteelBlue1', 'DarkSeaGreen1', 'Khaki1'] 10 | self.item_count = len(self.item_colors) 11 | self.default_item_count = 4 12 | 13 | self.master = master 14 | self.pack(fill=tk.BOTH, expand=True) 15 | self.layout_panels() 16 | self.layout_control_panel() 17 | self.layout_items() 18 | 19 | def layout_panels(self): 20 | self.simulation_panel = tk.Frame(self, bg = "white") 21 | self.control_panel = tk.Frame(self) 22 | self.control_panel.pack(side=tk.BOTTOM) 23 | self.simulation_panel.pack(side=tk.TOP,expand=True, fill=tk.BOTH) 24 | self.contents_frame = tk.Frame(self.simulation_panel, bg="gray31") 25 | self.contents_frame.pack(side="top", fill="both",expand=True) 26 | 27 | def layout_control_panel(self): 28 | tk.Label(self.control_panel, text="pack").grid(column=0, row=1,sticky="news") 29 | tk.Label(self.control_panel, text="side").grid(column=0, row=2,sticky="news") 30 | tk.Label(self.control_panel, text="fill").grid(column=0, row=3,sticky="news") 31 | tk.Label(self.control_panel, text="expand").grid(column=0, row=4,sticky="news") 32 | for x in range(1,self.item_count+1): 33 | tk.Label(self.control_panel, text="Item " + str(x)).grid(column=x,row=0) 34 | 35 | self.sides = [] 36 | self.sides.append(tk.StringVar()) 37 | self.sides[0].set("top") 38 | 39 | self.fills = [] 40 | self.fills.append(tk.StringVar()) 41 | self.fills[0].set("none") 42 | 43 | self.creates = [] 44 | self.creates.append(tk.BooleanVar()) 45 | self.creates[0].set(False) 46 | 47 | self.expands = [] 48 | self.expands.append(tk.BooleanVar()) 49 | self.expands[0].set(False) 50 | 51 | for y in range(1,self.item_count+1): 52 | self.sides.append(tk.StringVar()) 53 | self.sides[y].set("top") 54 | cb1 = ttk.Combobox(self.control_panel,textvariable=self.sides[y],state="readonly",values=['top', 'bottom', 'left', 'right'], width=7) 55 | cb1.bind("<>", self.layout_items) 56 | cb1.grid(column=y,row=2) 57 | 58 | self.fills.append(tk.StringVar()) 59 | self.fills[y].set("none") 60 | cb2 = ttk.Combobox(self.control_panel,textvariable=self.fills[y],state="readonly",values=['none', 'x', 'y', 'both'], width=7) 61 | cb2.bind("<>", self.layout_items) 62 | cb2.grid(column=y,row=3) 63 | 64 | self.creates.append(tk.BooleanVar()) 65 | self.creates[y].set(y <= self.default_item_count) 66 | 67 | 68 | chk1 = ttk.Checkbutton(self.control_panel,variable=self.creates[y],command=self.layout_items) 69 | chk1.grid(column=y,row=1) 70 | 71 | self.expands.append(tk.BooleanVar()) 72 | self.expands[y].set(False) 73 | chk2 = ttk.Checkbutton(self.control_panel,variable=self.expands[y],command=self.layout_items) 74 | chk2.grid(column=y,row=4) 75 | 76 | 77 | def layout_items(self,*args): 78 | self.contents_frame.pack_forget() 79 | for child in self.contents_frame.winfo_children(): 80 | child.destroy() 81 | self.contents_frame.pack(side="top", fill="both", expand=True) 82 | self.items = [] 83 | self.items.append(tk.Label(self.contents_frame)) 84 | for x in range(1,self.item_count+1): 85 | self.items.append(tk.Label(self.contents_frame)) 86 | self.items[x] = tk.Label(self.contents_frame) 87 | self.items[x]["text"] = "Item " + str(x) 88 | self.items[x]["bg"] = self.item_colors[x-1] 89 | if self.creates[x].get(): 90 | self.items[x].pack(side=self.sides[x].get(), fill=self.fills[x].get(), expand=self.expands[x].get()) 91 | 92 | def say_hi(self): 93 | print("hi there, everyone!") 94 | 95 | root = tk.Tk() 96 | app = Application(master=root) 97 | app.mainloop() 98 | --------------------------------------------------------------------------------