簡體   English   中英

Tkinter 滾動條離屏且不滾動

[英]Tkinter scrollbar is off the screen and doesn't scroll

所以我創建了一個 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()

在互聯網上找不到任何東西,所以任何幫助都將不勝感激。 先感謝您。

您正在 canvas ( self.canvas ) 內創建畫布 ( ResultScrollable ) 並在這些畫布上使用pack() 所以ResultScrollable.bbox('all')不會包括那些畫布。 您應該創建一個內部Frame並將其與 create_window 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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM