使用滚动条时图像会消失

时间:2019-06-03 15:45:22

标签: python image tkinter scrollbar

提前为语法错误道歉!

我正在使用tkinter进行学校项目。 这个问题让我发疯了好几天了! 我试图在画布上放置滚动条,在画布上放置框架,但是每次滚动一些标签时,图片就会消失。

如果调整窗口大小,图像和标签将立即返回。 注意:我并行处理多个文件和类,只添加了与该问题有关的文件。

class Feed(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        main_feed=tk.Label(self,bg="#F7F7F7")
        main_feed.pack(fill=tk.BOTH,side=tk.TOP,expand=tk.YES)

        frame_frame=MainFrame(main_feed, self.controller)
        frame_frame.pack(fill=tk.BOTH,expand=tk.YES)

        right_side_layer=tk.Label(frame_frame.frame_background,height=10,width=8,bg="#F7F7F7")
        right_side_layer.pack(fill=tk.BOTH,side=tk.RIGHT,expand=tk.YES)

        self.line_index=tk.IntVar(self,0)
        self.old_width_canvas= tk.IntVar(self,0)

        self.post_canvas = tk.Canvas(right_side_layer,bg="#F7F7F7")
        vbar=tk.Scrollbar(self.post_canvas , orient=tk.VERTICAL, command= self.custom_yview)

        self.post_canvas.bind('<Configure>', self.On_Configure)
        self.post_canvas.configure(yscrollcommand=vbar.set)

        self.post_canvas.pack(fill=tk.BOTH, expand=tk.YES)


    # Helping function for the scrolbar
    def custom_yview(self ,*args, **kwargs):
        self.post_canvas.yview(*args, **kwargs)
        x = self.post_canvas.canvasx(0)
        y = self.post_canvas.canvasy(0)
        self.post_canvas.coords("bg", x, y)

    def On_Configure(self,event,*args, **kwargs):
        self.custom_yview(*args, **kwargs)
        self.old_width_canvas.set(self.post_canvas.winfo_width())
        height = self.line_index.get()
        if height > self.post_canvas.winfo_height():
            self.post_canvas.bind_all("<MouseWheel>",lambda event:self.Update_Position(event, self.post_canvas))
            self.post_canvas.configure(scrollregion=(0,0,self.post_canvas.winfo_width(), height)) # (x0, y0, width, height)
        else:
            self.post_canvas.unbind_all("<MouseWheel>")

    def Update_Position(self, event, screen,*args, **kwargs):
        screen.yview_scroll(-1*(event.delta/120), "units")
        self.custom_yview(*args, **kwargs)
        self.frames['Feed'].post_canvas.create_window(100,num,window = new_post)
        num+=150
        self.frames['Feed'].line_index.set(self.frames['Feed'].line_index.get()+150)
        self.frames['Feed'].post_canvas.configure(scrollregion=(0,0,self.frames['Feed'].post_canvas.winfo_width(), 
        self.frames['Feed'].line_index.get())) # (x0, y0, width, height)

谢谢!

0 个答案:

没有答案