Python3-错误的窗口路径名

时间:2019-09-23 19:57:27

标签: python-3.x tkinter

我的GUI有2个画布,每个画布都有一个滚动条。使用画布1时没有任何错误。我可以将帧添加到画布2中而没有任何错误。该GUI工作正常,在使用时不会引发任何错误。我在canvas2中添加了一个框架,该框架将用于显示时间窗口。时间窗口是一个可以完成其预期功能的绘图。我无法像标签一样将其他内容添加到time_box框架中。关闭主窗口时,我还会收到以下错误。

Exception in Tkinter callback
Traceback (most recent call last):
  File "D:\Users\r0subr05\Anaconda3\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "<ipython-input-37-21bd9a040da1>", line 138, in on_configure
    self.width = self.canvas1.winfo_width()
  File "D:\Users\r0subr05\Anaconda3\lib\tkinter\__init__.py", line 1161, in winfo_width
    self.tk.call('winfo', 'width', self._w))
_tkinter.TclError: bad window path name ".!canvas"
Exception in Tkinter callback
Traceback (most recent call last):
  File "D:\Users\r0subr05\Anaconda3\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "D:\Users\r0subr05\Anaconda3\lib\tkinter\__init__.py", line 3069, in set
    self.tk.call(self._w, 'set', first, last)
_tkinter.TclError: invalid command name ".!scrollbar"\

我正在使用的代码是:

import tkinter as tk
import pandas as pd

import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import rcParams


df_dict = dict()
df_plotframes = dict()
df_chkvar = dict()
default_cat_list = ["Category_1"]
n = len(default_cat_list)
rcParams.update({'figure.autolayout': True})


class tsv(tk.Tk):
    def __init__(self,root,*args, **kwargs):
        tk.Frame.__init__(self, root, *args, **kwargs)
        self.root = root
        self.root.geometry('{}x{}'.format(800, 700))

        # layout all of the main containers
        self.root.grid_rowconfigure(1, weight=1)
        self.root.grid_columnconfigure(0, weight=1)        
        self.root.title("Time-series Visualization")


        #Menu bar
        self.menu = tk.Menu(self.root) 
        self.root.config(menu=self.menu) 
        self.filemenu = tk.Menu(self.menu) 
        self.menu.add_cascade(label='File', menu=self.filemenu)        

        self.filemenu.add_separator() 
        self.filemenu.add_command(label='Exit', command=self.root.destroy) 

        self.helpmenu = tk.Menu(self.menu) 
        self.menu.add_cascade(label='Help', menu=self.helpmenu) 
        self.helpmenu.add_command(label='About') 

         #Global variables
        self.category = tk.StringVar()
        self.rownum = 0
        self.mindatetime = pd.to_datetime('1900-01-01 00:00:00')
        self.maxdatetime = pd.to_datetime('1900-01-01 00:00:00')

        self.zmin = pd.to_datetime('1900-01-01 00:00:00')
        self.zmax = pd.to_datetime('1900-01-01 00:00:00')
        self.span_count = 1

        #Top frame
        self.top_frame = tk.Frame(self.root)
        self.top_frame.grid(row=0, column=0, sticky='nsew')

        self.category_name = tk.Label(self.top_frame, text='Add Category:')
        self.category_name.grid(row=1, column=0, sticky='nsew')

        self.category.set(default_cat_list[len(default_cat_list)-1])

        self.entry_category = tk.Entry(self.top_frame, background="pink",textvariable = self.category)
        self.entry_category.grid(row=1, column=1, sticky='nsew')
        self.entry_category.focus_set()

        self.ok_button = tk.Button(self.top_frame, text="OK", command=lambda e = self.entry_category:self.enterCategory(e))
        self.ok_button.grid(row=1, column=2, sticky='nsew')

        #Canvas for scrollable frame
        self.xscrollbar = tk.Scrollbar(self.root, orient='horizontal')
        self.xscrollbar.grid(row=3, column=0, sticky='ew')

        self.yscrollbar = tk.Scrollbar(self.root)
        self.yscrollbar.grid(row=1, column=1, sticky='ns')

        self.canvas1 = tk.Canvas(self.root, bd=0, yscrollcommand=self.yscrollbar.set)
        self.canvas1.grid(row=1, column=0, sticky='nsew')

        # create the center widgets
        self.canvas1.grid_rowconfigure(1, weight=1)
        self.canvas1.grid_columnconfigure(0, weight=1)
        self.yscrollbar.config(command=self.canvas1.yview)

        #### time zoom box
        self.canvas1.config(scrollregion=self.canvas1.bbox("all"))

        self.canvas2 = tk.Canvas(self.root, bd=0,height=150,xscrollcommand=self.xscrollbar.set)

        self.canvas2.grid(row=2, column=0, sticky='nsew')
        self.canvas2.grid_rowconfigure(2, weight=1)
        self.canvas2.grid_columnconfigure(0, weight=1)

        self.xscrollbar.config(command=self.canvas2.xview)

        self.canvas2.config(scrollregion=self.canvas2.bbox("all"))

        self.main_frame = tk.Frame(self.canvas1, height=550)#, background="bisque", height=200)
        self.canvas1.create_window((0,0), window=self.main_frame,anchor='nw', tags=("main_frame",))

        self.time_box = tk.Frame(self.canvas2)
        self.canvas2.create_window((0,0), window=self.time_box,anchor='nw', tags=("time_box",))

        self.root.bind('<Configure>', self.on_configure) 


        self.figure = plt.Figure(figsize=(8,2))
        self.ax = self.figure.add_subplot(111, facecolor='white')                       
        self.line = FigureCanvasTkAgg(self.figure, self.time_box)                      
        self.line.get_tk_widget().pack(side="top", fill="both",expand=True)
        self.x = [self.mindatetime,self.maxdatetime]

        self.y = [1] *len(self.x)

        self.ax.plot(self.x, self.y, '-')
        self.ax.set_title('Press left mouse button and drag to set time window')
        self.ax.set_yticklabels([])
        #For lines in the entire plot
        for xc in self.x:
            self.ax.axvline(x=xc, color='grey', linestyle='-')

    def on_configure(self,event):
        # update scrollregion after starting 'mainloop'
        self.width = self.canvas1.winfo_width()
        self.width2 = self.canvas2.winfo_width()

        self.canvas1.itemconfigure("main_frame", width=self.width)
        self.canvas2.itemconfigure("time_box", width=self.width2)

        self.canvas1.configure(scrollregion=self.canvas1.bbox('all'))
        self.canvas2.configure(scrollregion=self.canvas2.bbox('all'))


if __name__ == "__main__":
    root = tk.Tk()

    gui = tsv(root)
    gui.mainloop()

0 个答案:

没有答案