Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Tkinter - change the canvas size after inital declaration

Tags:

python

tkinter

I want to change the canvas size after I have added some widgets to it

Example:

from Tkinter import * 

master = Tk()
w = Canvas(master, width=100, height=100)
w.config(bg='white')
w.create_oval(90,90,110,110, width=0, fill = "ivory3")
w = Canvas(master, width=200, height=200)
w.pack()
mainloop()

But it seem that when I re-declare the canvas size, the objects get removed. Is it possible to update the canvas after I have created some objects on it?

like image 471
Jay Gattuso Avatar asked Mar 06 '12 18:03

Jay Gattuso


People also ask

What is difference between frame and Canvas in tkinter?

A Frame is designed to be a parent container of other widgets. A Canvas is like a canvas that you can draw somethings on it like lines, circles, text, etc. A Canvas can be used as a parent container as well but it is not designed for that at the first place.

How do you change a Canvas image in Python?

Build A Paint Program With TKinter and Python We can add a button to trigger the event which when pressed will change the canvas image. To change a particular image, we can configure the canvas by using the itemconfig() constructor. It takes image files which need to be updated and displayed them on the window.

What is BBOX in tkinter?

c.bbox(item) Returns an approximate bounding box for item, a tuple of four integers: the pixel coordinates of minimum x, minimum y, maximum x, maximum y, in this order.

Can you put a frame on a Canvas tkinter?

Practical Data Science using Python The Canvas is a rectangular area intended for drawing pictures or other complex layouts. You can place graphics, text, widgets or frames on a Canvas.


1 Answers

What you are looking for is the configure option, as is documentered here. Basically, something like this should help, in place of creating a new canvas:

w.config(width=200, height=200)

For reference, the reason why everything got deleted off of the Canvas is because you created a brand new Canvas, with a different size and the same name. If you are going to change properties of an existing object, you must change the existing object, and not overwrite it. Basically you overwrite something if you declare it equal to something else (w=Canvas(...)).

like image 103
PearsonArtPhoto Avatar answered Oct 20 '22 20:10

PearsonArtPhoto