I just want to start by saying that I am pretty new to coding with tkinter and Python. Hope you can help me out.
I am trying to create an application with a help section that is supposed to be opened as a Toplevel with tkinter when clicking a button. The Toplevel needs to be scrollable AND wrap the text as I resize the Toplevel window.
Below is a working code which gives me a good scrollable window but won't wrap the text. The Expl(300, 300) at the bottom is in the original setup called from another Python file which has a main tk.Tk()-window. Can anyone give me advice on how the wrap functionality works, and what I am doing wrong right now?
import tkinter as tk
WIDTH = 500
HEIGHT = 500
BG = '#fff'
class Expl(tk.Frame):
def __init__(self, x, y):
self.x = x
self.y = y
self.width = round(WIDTH/2)
self.height = round(HEIGHT/1.8)
self.window = tk.Toplevel()
self.window.wm_geometry('%dx%d+%d+%d' % (self.width, self.height, max(0, self.x - (WIDTH/2 - WIDTH/2/2)), max(0, self.y - (HEIGHT/2 - HEIGHT/1.8/2))))
self.window.title('Förklaringar')
self.window.configure(background=BG)
#self.window.iconbitmap(default=ICON_PATH)
tk.Frame.__init__(self, self.window)
myframe=tk.Frame(self.window, width=self.width, height=self.height, bd=1, background=BG)
myframe.place(x=10,y=10)
self.canvas = tk.Canvas(self.window, borderwidth=0, background=BG)
self.frame = tk.Frame(self.canvas, background=BG)
self.vsb = tk.Scrollbar(self.window, orient="vertical", command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.vsb.set)
self.canvas.bind_all("<MouseWheel>", self.onmousewheel)
self.frame.bind("<Configure>", self.myfunction)
self.frame.bind("<Configure>", self.onFrameConfigure)
self.vsb.pack(side="right", fill="y")
self.canvas.create_window((0,0),window=self.frame,anchor='nw')
self.canvas.pack(side="left", fill="both", expand=True)
self.window.grid_rowconfigure(0, weight=1)
self.window.grid_columnconfigure(0, weight=1)
self.draw()
def myfunction(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
def onmousewheel(self, event):
self.canvas.yview_scroll(int(-1*(event.delta/120)), "units")
def onFrameConfigure(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
def draw(self):
# Titles
tk.Label(self.frame, text='TEST 1').grid(row=0, column=0, sticky='SW', padx=10, pady=(5, 0))
tk.Label(self.frame, text='TEST 2').grid(row=1, column=0, sticky='SW', padx=10, pady=(5, 0))
tk.Label(self.frame, text='TEST 3').grid(row=2, column=0, sticky='SW', padx=10, pady=(5, 0))
texxt = WrappingLabel(self.frame, text="TESTTTTTT TTTTTTTT T IN RSG RSG SRG RSG")
texxt.grid(row=3, column=0, sticky='SW', padx=10, pady=(5, 0))
class WrappingLabel(tk.Label):
def __init__(self, master=None, **kwargs):
tk.Label.__init__(self, master, **kwargs)
self.bind('<Configure>', lambda _: self.config(wraplength=master.winfo_width()))
Expl(300, 300)


