【问题标题】:Python tkinter unable to add scrollbar to figure embedded in canvasPython tkinter 无法将滚动条添加到嵌入画布中的图形
【发布时间】:2018-06-14 21:46:05
【问题描述】:

我正在开发一个程序,它 1) 在文件夹中打开图像 2) 将所有这些图像整合到一个图形中 3) 将图形插入到画布中。我原来的问题是,在编译的图中,所有的图片都太小了,看不到。所以,我想一次显示一张图片/图形的一部分,然后向下滚动到下一张。

我当前的问题是我无法在图形/画布中插入滚动条。我得到的错误是:

scrollbar = Scrollbar(canvas)

AttributeError: 'FigureCanvasTkAgg' object has no attribute 'tk'

我读到滚动条只能添加到框架中,但是当我尝试将框架添加到画布时,我得到了同样的错误。我的代码如下:

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    from Tkinter import *
    import ttk
except:
    from tkinter import ttk
    from tkinter import *
    from tkinter.ttk import *

import tkMessageBox
import os

import matplotlib
matplotlib.use("TkAgg")

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import numpy as np

LARGE_FONT = ("Verdana", 12)

class Project(tk.Tk):

    #Base line code to initialize everything 

    def __init__(self, *args, **kwargs):        
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "Project")

        container = tk.Frame(self) #Define frame/edge of window
        container.pack(side="top", fill="both", expand=True) #fill will fill space you have allotted pack. Expand will take up all white space.
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1) #0 sets minimum size weight sets priority

        self.frames = {}

        for F in [StartPage]:
            frame = F(container, self)
            self.frames[F] = frame 
            frame.grid(row=0, column=0, sticky="nsew") 

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise() #raise to front


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text = "Figure Page!", font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        #add subplot with all images in folder 'PictureFolder'
        f = Figure(figsize = (8,8), dpi=100)#define figure      
        i=1
        for picture in  os.listdir("/home/pi/Desktop/PictureFolder/"):
            a = f.add_subplot(6,1,i) #add subplot RCP. Pth pos on grid with R rows and C columns
            img = mpimg.imread("/home/pi/Desktop/PictureFolder/" + picture) #read in image
            a.imshow(img) #Renders image
            i+=1

        #add canvas which is what we intend to render graph to and fill it with figure
        canvas = FigureCanvasTkAgg(f, self) 
        #frame2 = tk.Frame(canvas)
        canvas.draw() #raise canvas
        canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.X, expand=True) #Fill options: BOTH, X, Y Expand options:  

        #Add figure toolbar
        toolbar = NavigationToolbar2Tk(canvas, self) #add traditionalmatplotlib toolbar
        toolbar.update()
        canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)


        #Add scrollbar
        scrollbar = Scrollbar(canvas)
        scrollbar.config(command=canvas.yview)

        scrollbar.pack(side=RIGHT, fill = Y)
        canvas.pack(side=LEFT, expand = YES, fill=BOTH)

        canvas.config(yscrollcommand=scrollbar.set)





app = Project()
app.geometry("640x480")
app.mainloop()

mainloop()

【问题讨论】:

  • 您的导入存在大问题。我敢打赌这是你问题的一部分。
  • 你能详细说明迈克吗?如果我只注释掉滚动条部分,代码在相同的导入下工作得很好。
  • 也许代码有效,但您的导入相互覆盖。这将导致问题。

标签: python matplotlib tkinter


【解决方案1】:

我想通了。代码中所有canvas对象都需要替换成canvas.get_tk_widget()

【讨论】:

    猜你喜欢
    • 2019-06-04
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    相关资源
    最近更新 更多