'How to resize pictures in scrollable frame (Tkinter, Python)
I created scrollable frame which contains several plots. How can I set it up in such a way when I resize the window, then the plots also will resize appropriately to fill all the space and the position of scrollbar will also changed? So far I set constant width and height for plots/frame and when I resize the windows then scrollbars stays in the same place. Thats my code:
import pandas as pd
import numpy as np
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
from tkscrolledframe import ScrolledFrame
class Graph(tk.Frame):
def __init__(self, master=None, title="", *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.fig = Figure(figsize=(4, 3))
ax = self.fig.add_subplot(111)
df = pd.DataFrame({"values": np.random.randint(0, 50, 10)}) #dummy data
df.plot(ax=ax)
self.canvas = FigureCanvasTkAgg(self.fig, master=self)
self.canvas.draw()
tk.Label(self, text=f"Graph {title}").grid(row=0)
self.canvas.get_tk_widget().grid(row=1, sticky="nesw")
toolbar_frame = tk.Frame(self)
toolbar_frame.grid(row=2, sticky="ew")
NavigationToolbar2Tk(self.canvas, toolbar_frame)
root = tk.Tk()
sf = ScrolledFrame(root, width = 1000, height = 1000)
sf.grid(row = 0, column = 0)
sf.bind_arrow_keys(root)
sf.bind_scroll_wheel(root)
inner_frame = sf.display_widget(Frame)
for num, i in enumerate(list("ABC")):
Graph(inner_frame, title=i, width=200).grid(row=num//2, column=num%2)
root.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
