1

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() 
9
  • I don't understand what this means: "I dont like that I can see how the scrollbar is moveing from 0 to 1". What does "moving from 0 to 1" mean? Can you describe that in a different way? Commented Jul 21, 2020 at 15:08
  • If you click on the add button, my code tells the scrollbar to move the slider at position 0, cause I need the percentage of hidden region. Then I want to show the user the end of the rectangel, so the slider of scrollbar (hscrbar) on the position 1(all the way right). Commented Jul 21, 2020 at 15:11
  • You need to click the button some often to see what happens. It is like flickering. Commented Jul 21, 2020 at 15:13
  • By "slider" are you referring to the thumb (the thing inside the scrollbar that you grab to scroll around)? It's also not clear if you want it to move when you add or if you don't want it to move. Commented Jul 21, 2020 at 15:14
  • 1
    It's not clear what you want get_width to do. Is it supposed to return how wide the drawing is? I don't understand why you aren't just calling bbox("all") to get the extent of the existing drawing. Most likely, the cause of your problem is that you're calling update_idletasks since that causes the display to refresh. Commented Jul 21, 2020 at 15:24

1 Answer 1

2

My guess is that the problem is largely due to the fact that inside of get_width you're moving the scrollbar, calling update_idletasks, and then moving the scrollbar again. That call to update_idletasks causes the window to redraw. That redraw means you'll see the scrollbar move to the left, and then it will move back to the right when the function is finished.

It's not entirely clear what get_width is supposed to do, but I'm guessing you can remove all of that code and replace it with self.Display.bbox("all"), and then grabbing the x coordinates from the result to compute the width of the drawing.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.