【问题标题】:Tkinter scrollbar is off the screen and doesn't scrollTkinter 滚动条不在屏幕上并且不滚动
【发布时间】:2020-04-14 16:03:41
【问题描述】:

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

在互联网上找不到任何东西,因此非常感谢任何帮助。提前谢谢你。

【问题讨论】:

  • 滚动条永远不应高于其父级。如果滚动区域的高度小于画布的高度,滚动条将被禁用。
  • 画布的滚动区域在填充后大于其父框架。滚动条不仅被禁用,而且被切断。
  • 由于您的代码没有缺失部分就无法执行,请将您的代码编辑为minimal reproducible example
  • here 是一个基于您发布的缺少部分代码的应用程序:RightPaneGraph(...)。显示滚动条并且滚动正在工作。
  • 我接受了您的建议,并重构了我的代码,使其具有最低程度的可重现性,但它仍然无法正常工作(我查看了您的粘贴箱,感谢您抽出宝贵时间提供帮助)。我仍然不知道为什么它不起作用。

标签: python tkinter scrollbar tk tkinter-canvas


【解决方案1】:

您正在画布 (ResultScrollable) 内创建画布 (self.canvas) 并在这些画布上使用 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))

【讨论】:

    猜你喜欢
    • 2015-04-03
    • 2016-07-07
    • 1970-01-01
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    相关资源
    最近更新 更多