In Tkinter, resizing a canvas and/or frame can be done using
canvas.pack(fill="both", expand=True)
This way I can drag the tkinter window with the mouse and the canvas and frames within will adapt to the new size.
However I have not found a solution for applying this to images within the canvas. Only solutions so far are to independently change the size of the images through event actions.
Is there any way to make images within a canvas to resize dynamically, just like the canvas does with the one-liner above?
Is there any way to make images within a canvas to resize dynamically, just like the canvas does with the one-liner above?
No, there is no way to do what you want. Images aren't like widgets which can automatically grow and shrink. You will need to set up a binding on the <Configure> event of the containing widget, and in the bound function you will have to convert the image to the desired size.
Related
I currently have a scrollbar and a canvas on the same hierarchical level. In the canvas, there is a frame created using the canvas' create_window method.
I have a binding that is called when the canvas is configured that will resize the scrollregion to fit bbox("all"). It works, but ONLY when the entire window is resized (e.g. If I add more widgets to the canvas that are now not in its visible region, I have to resize the window to be able to change the canvas' scrollregion).
Ideally, the scrollregion should change as soon as the new widget is added to a nonvisible location of the canvas (e.g. it's off the screen). What am I currently doing incorrectly? Any advice is appreciated!
If I am reading your mind correctly (you really need to include a Minimal, Complete, Verifiable Example with debugging questions!), you are recalculating the scrollregion only on receipt of a <Configure> event. That only triggers when the widget changes size - and calling .create_window() on a Canvas certainly doesn't change its size. The simplest solution would probably be to explicitly do the scrollregion recalc yourself, every time you add widgets to it (there's no event that is triggered by this action, as far as I know). You might need to call .update_idletasks() first, to give the newly-added widget a chance to calculate its own size.
You need to put a binding on the inner frame's <Configure> event to also reset the scrollregion.
I was wondering if it is possible to save a canvas that had several textures painted on it as an image file.
I know I can save regular Image's (kivy.core.image) or Texture's (kivy.graphics.texture) as an image file with the save() function, so if I am able to convert the canvas to an Image or a Texture it should be easy, but so far I wasn't able to do this.
Widgets have an export_to_png method. Call this from the Widget whose canvas you have drawn on.
The problem is that your MyPainter widget has its default size of (100,100) and its default position (0,0). So it is actually behind your Save Button. So all your drawing is actually outside the MyPainter widget, and saving the widget to an image is blank.
The fix is to change the pos and size of MyPainter, and perhaps use collide_point() in your on_touch_down() and on_touch_move() methods to ensure that you are actually drawing on the MyPainter widget.
Someone know if it's possible to change the color of a pixel in a canvas without using un object, so without using something like canvas.create_oval or canvas.create_rectangle ?
There is no way to color a pixel other than to create a 1x1 pixel object of some sort. And yes, at some point you will experience performance problems. The canvas simply wasn't designed to be used this way.
If you're really needing to create a large area in which you can manage individual pixels, you can create a canvas with a single image that is the same size as the canvas. You can then set the color of individual pixels on the image through the photo image interface.
Within tkinter itself, it's impossible.
Even if you manage to change a pixel on canvas window (which is possible with X11 and Windows APIs in a platform-dependent way), you'd have to ensure it's repainted properly.
You can, of course, place a frame of size 1x1 over the canvas, with a background color you want. This way, pixel is "changed" and no canvas object is created. If there's a real (though strange) problem behind a question, this trick could be a solution.
Ideally, the transparent border.
Here's an example of what i'd like to achieve:
Notice the transparent border.
Now i suppose I could use cairo to create a rectangle with transparency, and put a borderless non-transparent window inside, mimic'ing that effect - which I would if i knew the window would have a fixed dimension. However, if the inner window grows, it'll grow out of the transparent rectangle.
How should one approach such task?
Making window frames is really the job of the window manager (at least under X11, don't know how it works on windows).
But have a look at the GtkBin, GtkBox or GtkMisc widgets. Pack the dialog inside it as a single widget, and use padding to give it a size. Read up on GTK+ drawing model. You will probably need to set a flag and define your own expose-event handler to re-draw your frame.
I have been making a small program with the Tkinter module in python, and I was wondering whether it was possible or not to resize a frame in my program with the mouse. As in, the user can drag the frame border and it will resize itself.
Your use of terminology makes the question unclear. Windows which may be resized by the user are called Toplevel windows. These are what appear as rectangular windows on the display, with a frame around them, typically a title bar, and edges or corners that can be grabbed and resized.
The term Frame refers to a container widget that must be inside a Toplevel or one of its descendents. A Frame has the ability to be resized but you have to write the code to let you interactively resize them. For example, you could place a little grip widget in one or more corners, and writing bindings to the press, motion and release of a mouse button.
Depending on the effect you are looking for, you might want a PanedWindow which is a container that includes a sash that lets you adjust the proportion of space between two other widgets.