Tkinter滚动条不在屏幕上,无法滚动

问题描述 投票:0回答:1

因此,我创建了一个GUI,并且需要一个垂直滚动条,但是该滚动条似乎比其父框架高,并且不会滚动。我不知道为什么,但是这是到目前为止的代码:

import tkinter as Tk
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure

root = Tk.Tk()

RightPane = Tk.Frame(root)
RightPane.grid()

class Graph:
    #Draws and return a placeholder for a graph
    #@param parent is the Tk.Frame parent obj
    #@param title is the (string) title for the graph
    def __init__(self, parent, title=''):
        #Initialise graph
        self.title = title
        self.fig = Figure(figsize=(4,4))
        self.plot = self.fig.add_subplot()
        self.plot.set_title(self.title, fontsize=10)
        self.plot.set_ylim(top=1)
        self.plot.set_xlim(right=255)
        self.plot.set_ylabel("Certainty", fontsize=8)
        self.plot.set_xlabel("Pixel Value", fontsize=8)

        #Draw
        self.canvas = FigureCanvasTkAgg(self.fig, master=parent)
        self.canvas.get_tk_widget().pack()
        self.canvas.draw()

        return

#Result Graphs -------------------------------------------
ResultFrame = Tk.Frame(RightPane)
ResultFrame.grid_columnconfigure(0, weight=1)
ResultFrame.grid(row=2, column=2, rowspan=14, padx=(10,0), pady=(10,0), sticky='nwe')

ResultScrollable = Tk.Canvas(ResultFrame)
ResultScrollable.grid(row=0, column=0, padx=(5,0), sticky='we')

graphCollection = []
for i in range(10):
    title = 'Certainty that image is digit: {}'.format(i)
    graphCollection.append(Graph(ResultScrollable, title=title))

ResultFrameVbar = Tk.Scrollbar(ResultFrame, orient='vertical', command=ResultScrollable.yview)
ResultFrameVbar.grid(row=0, column=1, sticky='nswe')

ResultScrollable.config(yscrollcommand=ResultFrameVbar.set, scrollregion=ResultScrollable.bbox('all'))

root.mainloop()

无法在互联网上找到任何东西,因此,我们将不胜感激。预先谢谢你。

python tkinter scrollbar tk tkinter-canvas
1个回答
0
投票

您正在画布(self.canvas)内创建画布(ResultScrollable),并在这些画布上使用pack()。因此,ResultScrollable.bbox('all')将不包括这些画布。您应该创建一个内部Frame并将其与create_window()关联,然后将这些画布放在内部Frame中:

internalFrame = Tk.Frame(ResultScrollable)
ResultScrollable.create_window(0, 0, window=internalFrame, anchor='nw')

graphCollection = []
for i in range(10):
    title = 'Certainty that image is digit: {}'.format(i)
    graphCollection.append(Graph(internalFrame, title=title))
© www.soinside.com 2019 - 2024. All rights reserved.