From 9980f03bdddbe622c00b62d9483920f530a97b67 Mon Sep 17 00:00:00 2001 From: Max Sponsel Date: Sat, 29 Aug 2020 10:10:11 +0200 Subject: [PATCH] Dynamische vertikale und horizontale Scrollbar MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Durch Hilfe von Stack Overflow wurde eine dynamische Scrollbar implementiert. Diese erscheint je nach Windowgröße dynamisch. (siehe hierzu: https://stackoverflow.com/questions/47008899/tkinter-dynamic-scrollbar-for-a-dynamic-gui-not-updating-with-gui) --- Code/Dyschromasie-Applikation.py | 87 +++++++++++++++++++++++++++----- 1 file changed, 74 insertions(+), 13 deletions(-) diff --git a/Code/Dyschromasie-Applikation.py b/Code/Dyschromasie-Applikation.py index ce7c8cb..86b0463 100644 --- a/Code/Dyschromasie-Applikation.py +++ b/Code/Dyschromasie-Applikation.py @@ -162,10 +162,70 @@ class Tritanopie(Dyschromasie): return self.sim_image +class AutoScrollbar(tk.Scrollbar): + # A scrollbar that hides itself if it's not needed. + # Only works if you use the grid geometry manager! + def set(self, lo, hi): + if float(lo) <= 0.0 and float(hi) >= 1.0: + # grid_remove is currently missing from Tkinter! + self.tk.call("grid", "remove", self) + else: + self.grid() + tk.Scrollbar.set(self, lo, hi) + def pack(self, **kw): + raise TclError("cannot use pack with this widget") + def place(self, **kw): + raise TclError("cannot use place with this widget") + +class ScrollFrame: + def __init__(self, master): + + self.vscrollbar = AutoScrollbar(master) + self.vscrollbar.grid(row=0, column=1, sticky='ns') + self.hscrollbar = AutoScrollbar(master, orient='horizontal') + self.hscrollbar.grid(row=1, column=0, sticky='ew') + + self.canvas = tk.Canvas(master, yscrollcommand=self.vscrollbar.set, + xscrollcommand=self.hscrollbar.set) + self.canvas.grid(row=0, column=0, sticky='nsew') + + self.vscrollbar.config(command=self.canvas.yview) + self.hscrollbar.config(command=self.canvas.xview) + + # make the canvas expandable + master.grid_rowconfigure(0, weight=1) + master.grid_columnconfigure(0, weight=1) + + # create frame inside canvas + self.frame = tk.Frame(self.canvas) + self.frame.rowconfigure(1, weight=1) + self.frame.columnconfigure(1, weight=1) + + # update the frame + self.frame.bind("", self.reset_scrollregion) + + def reset_scrollregion(self, event): + self.canvas.configure(scrollregion=self.canvas.bbox('all')) + + def update(self): + self.canvas.create_window(0, 0, anchor='nw', window=self.frame) + self.frame.update_idletasks() + self.canvas.config(scrollregion=self.canvas.bbox("all")) + + if self.frame.winfo_reqwidth() != self.canvas.winfo_width(): + # update the canvas's width to fit the inner frame + self.canvas.config(width = self.frame.winfo_reqwidth()) + if self.frame.winfo_reqheight() != self.canvas.winfo_height(): + # update the canvas's width to fit the inner frame + self.canvas.config(height = self.frame.winfo_reqheight()) + root = tk.Tk() + root.title("Projekt Dyschromasie") +SB = ScrollFrame(root) + img = np.array([]) rows = 0 cols = 0 @@ -176,7 +236,7 @@ sim_deut = tk.IntVar(root) sim_tri = tk.IntVar(root) simGrad = tk.IntVar(root) -simulationsGradient = tk.Scale(root, from_=0, to_=100, variable=simGrad, orient='horizontal') +simulationsGradient = tk.Scale(SB.frame, from_=0, to_=100, variable=simGrad, orient='horizontal') simulationsGradient.grid(column= 0, row = 1, columnspan=10) def browse(): @@ -192,7 +252,7 @@ def browse(): # Anzeigen des Bildes tkimage = ImageTk.PhotoImage(im) - myvar = tk.Label(root, image=tkimage) + myvar = tk.Label(SB.frame, image=tkimage) myvar.image = tkimage myvar.grid(columnspan=5) @@ -209,53 +269,54 @@ def simulate(): d = Deuteranopie(img, rows, cols, kanaele) display_array_deut = cv2.cvtColor(np.copy(d.Simulate()), cv2.COLOR_BGR2RGB) - T = tk.Text(root, height=1, width=15) + T = tk.Text(SB.frame, height=1, width=15) T.grid(columnspan=5) T.insert('current', "Deutranopie:") conv_SimulationPic_deut = ImageTk.PhotoImage(image=PIL.Image.fromarray(display_array_deut)) - sim_pic_deut = tk.Label(root, image=conv_SimulationPic_deut) + sim_pic_deut = tk.Label(SB.frame, image=conv_SimulationPic_deut) sim_pic_deut.Image = conv_SimulationPic_deut sim_pic_deut.grid(columnspan=5) elif sim_tri.get(): t = Tritanopie(img, rows, cols, kanaele) display_array_tri = cv2.cvtColor(np.copy(t.Simulate()), cv2.COLOR_BGR2RGB) - T = tk.Text(root, height=1, width=15) + T = tk.Text(SB.frame, height=1, width=15) T.grid(columnspan=5) T.insert('current', "Tritanopie:") conv_SimulationPic_tri = ImageTk.PhotoImage(image=PIL.Image.fromarray(display_array_tri)) - sim_pic_tri = tk.Label(root, image=conv_SimulationPic_tri) + sim_pic_tri = tk.Label(SB.frame, image=conv_SimulationPic_tri) sim_pic_tri.Image = conv_SimulationPic_tri sim_pic_tri.grid(columnspan=5) elif sim_pro.get(): p = Protanopie(img, rows, cols, kanaele) display_array_pro = cv2.cvtColor(np.copy(p.Simulate()), cv2.COLOR_BGR2RGB) - T = tk.Text(root, height=1, width=15) + T = tk.Text(SB.frame, height=1, width=15) T.grid(columnspan=5) T.insert('current', "Protanopie:") conv_SimulationPic_pro = ImageTk.PhotoImage(image=PIL.Image.fromarray(display_array_pro)) - sim_pic_pro = tk.Label(root, image=conv_SimulationPic_pro) + sim_pic_pro = tk.Label(SB.frame, image=conv_SimulationPic_pro) sim_pic_pro.Image = conv_SimulationPic_pro sim_pic_pro.grid(columnspan=5) -btn = tk.Button(root, text="Browse", width=25, command=browse, bg='light blue') +btn = tk.Button(SB.frame, text="Browse", width=25, command=browse, bg='light blue') btn.grid(column=0, row=0, columnspan=2) -simulateButton = tk.Button(root, text="Simulate", width=25, command=simulate, bg='light blue') +simulateButton = tk.Button(SB.frame, text="Simulate", width=25, command=simulate, bg='light blue') simulateButton.grid(column=1, row=0, columnspan=2) simulateButton.config(state='disabled') -checkButton_p = tk.Checkbutton(root, text="Protanop", variable=sim_pro, onvalue=1, offvalue=0, height=5, width=20) -checkButton_d = tk.Checkbutton(root, text="Deutanop", variable=sim_deut, onvalue=1, offvalue=0, height=5, width=20) -checkButton_t = tk.Checkbutton(root, text="Tritanop", variable=sim_tri, onvalue=1, offvalue=0, height=5, width=20) +checkButton_p = tk.Checkbutton(SB.frame, text="Protanop", variable=sim_pro, onvalue=1, offvalue=0, height=5, width=20) +checkButton_d = tk.Checkbutton(SB.frame, text="Deutanop", variable=sim_deut, onvalue=1, offvalue=0, height=5, width=20) +checkButton_t = tk.Checkbutton(SB.frame, text="Tritanop", variable=sim_tri, onvalue=1, offvalue=0, height=5, width=20) checkButton_p.grid(column=0, row=2) checkButton_d.grid(column=1, row=2) checkButton_t.grid(column=2, row=2) +SB.update() root.mainloop()