![tkinter get mouse coordinates on canvas tkinter get mouse coordinates on canvas](https://www.kosbie.net/cmu/fall-11/15-112/handouts/mickeyFractal.jpg)
Super()._init_(container, *args, **kwargs) Here's a ScrollableFrame class that you can just use as a normal frame! import tkinter as tkĭef _init_(self, container, *args, **kwargs): We've done it for you, so you don't have to!
#Tkinter get mouse coordinates on canvas code
To make creating scrollable frames a bit easier, you might be tempted to code up an easy to use class. Ttk.Label(scrollable_frame, text="Sample scrolling label").pack()Ĭanvas.pack(side="left", fill="both", expand=True) The final code might look something like this: import tkinter as tkĬanvas.create_window((0, 0), window=scrollable_frame, anchor="nw")Ĭnfigure(yscrollcommand=t) Once you've done this, you can add elements to the scrollable_frame, and it'll work as you'd expect!Īlso, don't forget to use Pack or Grid to add the container, canvas, and scrollbar to the application window. The argument anchor="nw" tells the canvas to place the frame's top left corner on position (0, 0).įinally, we have to configure the canvas so that when its y-position changes, the scrollbar moves: nfigure(yscrollcommand=t) The values (0, 0) tell the canvas on which position to draw the window. Next up we have to tell the canvas to actually draw the scrollable_frame inside itself: canvas.create_window((0, 0), window=scrollable_frame, anchor="nw") The change is we modify the canvas' scrollregion to have a size of canvas.bbox("all").Ĭalling canvas.bbox("all") gives us a 4-value tuple describing the position of two corners of a rectangle which is the scroll region. Only when it changes size do we want to change any of the scrolling properties. usually when we add or remove widgets from within it. The event we're binding here triggers whenever the scrollable_frame changes size-i.e. This is necessary because we must tell the canvas how large the frame will be, so that it knows how much it can scroll: scrollable_frame.bind( Now let's add some code that will call a function whenever the contents of the scrollable frame change. Scrollbar = ttk.Scrollbar(container, orient="vertical", command=canvas.yview) We will create it, giving it the canvas as its container, but instead of using Pack or Grid, we will using the canvas' create_window method to create a window within the canvas that shows the scrollable frame's content.Ĭreating the widgets is straightforward enough: import tkinter as tk However, the scrollable frame will behave differently. you place them in their container and then use Pack or Grid). A frame that will become the scrollable frameĪlmost all these widgets will work like normal Tkinter widgets (i.e.A container frame for our canvas and scrollbar.Let's start off by creating all the widgets we'll need. Often, the scrollable area matches the contents of the inner window-but it can also be different if we wish. In order to do so, we must also specify what the scrollable area will be. That means that we'll be able to set the Canvas size to whatever we want, and then when we scroll we'll be moving along the window inside it. The window can have as much content as we like, but it's the Canvas that we'll be placing inside our application.
![tkinter get mouse coordinates on canvas tkinter get mouse coordinates on canvas](https://1.bp.blogspot.com/-bJA0gpDYrj4/WZCKMlNFJAI/AAAAAAAAAtw/khCdzs1TLWsBjFNetluObQhUOtPfffOsQCLcBGAs/s1600/aplikasi%2Bpembuka%2Bfoto%2Btkinter%2B5.png)
What we'll be doing is creating a Canvas, and inside it creating a window. The scrollable frame is what the user sees, and you can think of it as a "window" that lets you see into the scrollable area. The content (with the darkest background) is the "actual size", which usually is also the "scrollable area".
![tkinter get mouse coordinates on canvas tkinter get mouse coordinates on canvas](https://i.stack.imgur.com/Hm5NU.png)
That means that it can have an actual size larger than the screen size, and you can move the area you are viewing. In Tkinter, only the Canvas widget is a natively scrollable container.