I’m trying to create a save/restore functionality in my Tkinter GUI, but I’m having trouble restoring the width/height of Widgets within a Paned Window. I’ve tried .place() and .config, but the resizable window around each of the widgets disappears and I can no longer resize each widget.
Example code:
from tkinter import *
root = Tk()
pw = PanedWindow(root)
text1 = Text(pw)
pw.add(text1)
text2 = Text(pw)
pw.add(text2)
entry1 = Entry(pw)
pw.add(entry1)
EDIT: No longer using TTK
EDIT2: No longer using Tkinter. Switched to PyQt5 and everything just works.
3
I’m not sure PaneWindows make sense singularly. You might need a minimum of 2, paired. Also, some bogus content helps for the widgets to have something to grasp to.
Here is my gadget.
from tkinter import *
from tkinter.ttk import *
root = Tk()
pw = PanedWindow(root)
pw.pack(fill=BOTH, expand=1)
text1 = Text(pw)
text1.insert(INSERT, "text1 .....")
pw.add(text1)
pw2 = PanedWindow(pw, orient=VERTICAL)
pw.add(pw2)
text2 = Text(pw)
text2.insert(INSERT, "text2 .....")
pw2.add(text2)
entry1 = Entry(pw)
pw.add(entry1)
Entry1 is in error as it’s parented to the first pane. All 3 windows should be parented by a 4th window.
3
You can save the position of sashes with pw.sashpos(i)
. Something like this:
def save():
sash_pos = []
i = 0
while True:
try:
sash_pos.append(pw.sashpos(i))
i += 1
except TclError:
break
with open("sash_pos.json", "w") as f:
json.dump(sash_pos, f)
root.destroy()
And then load them with pw.sashpos(i, pos)
like this:
def load():
try:
with open("sash_pos.json", "r") as f:
sash_pos = json.load(f)
print(sash_pos)
for i, pos in enumerate(sash_pos):
pw.sashpos(i, pos)
except FileNotFoundError:
pass
Just make sure window is fully loaded before calling the load
function. something like pw.after(100, load)
worked for me.
Just use PyQt5. Don’t bother with Tkinter (Or worse, the TTK library).