In my orignal code I'm doing something like below, just with much more math.
It works but I dont like that I can see how the thumb of my scrollbar (hscrbar) is moveing from position 0 to 1 while I calculate the width in get_width(self): of my rectangel. Cause in my original code I need to see it everytime I add something.
At the moment I havent an idea to solve this and you may are aware of a solution for it.
import tkinter as tk root = tk.Tk() class my_figure(tk.Frame): def __init__(self, master): tk.Frame.__init__(self, master) self.master = master self.root = self.winfo_toplevel() # DownFrame self.button = tk.Button(self, text='add', command=self.add) self.button.grid(column=0, row=0) self.body = tk.Frame(self, relief='sunken') self.hscrbar = tk.Scrollbar(self.body, orient=tk.HORIZONTAL) self.vscrbar = tk.Scrollbar(self.body) self.Display = tk.Canvas(self.body, xscrollcommand=self.hscrbar.set, yscrollcommand=self.vscrbar.set) self.hscrbar.config(command=self.Display.xview) self.body.grid(column=0, row=1, sticky='nswe') self.vscrbar.grid(column=1,sticky='ns') self.hscrbar.grid(row=1, column=0, sticky='we') self.Display.grid(column=0, row=0, sticky='nswe') self.vscrbar.config(command=self.Display.yview) self.hscrbar.config(command=self.Display.xview) self.x = tk.IntVar() self.y = tk.IntVar() self.x.set(10) self.y.set(10) self.height = 10 def add(self): self.Display.create_rectangle(self.x.get(),self.y.get(),self.get_width(),self.height) self.old_x = self.x.get() self.old_y = self.y.get() self.x.set(self.old_x+40) self.y.set(self.old_y+20) self.Display.config(scrollregion=self.Display.bbox("all")) def get_width(self): if self.hscrbar.get()[0] == 0 and self.hscrbar.get()[1] == 1: #if scrollbar shows everything return self.Display.winfo_width()#return width of the canvas else: self.Display.xview_moveto(0) #scrollbar at postition 0 self.root.update_idletasks() #update idletasks to get correct value value = self.Display.winfo_width()-round(self.Display.winfo_width()*self.hscrbar.get()[1]) width = value+self.Display.winfo_width() #calculate the width self.Display.xview_moveto(1) #move to position 1 to show my the end of rectangel return width figure = my_figure(root) figure.grid() root.mainloop()
get_widthto do. Is it supposed to return how wide the drawing is? I don't understand why you aren't just callingbbox("all")to get the extent of the existing drawing. Most likely, the cause of your problem is that you're callingupdate_idletaskssince that causes the display to refresh.