Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide and show canvas items on tkinter?

I know that pack and pack_forget can be used to show or hide widgets. Which are the equivalent commands for items on a canvas?

Notice: It would be better to keep the complete item information, including its position. This is not the case with pack/pack_forget, where you need to inform the position again whenever you use pack.

like image 705
bmello Avatar asked Nov 27 '18 12:11

bmello


People also ask

Can you make a Canvas transparent tkinter?

A transparent ColorKey can be set with SetLayeredWindowAttributes while we just use LWA_COLORKEY the alpha parameter has no use to us. Important note: After defining a transparent colorkey, everything in that canvas with that color will be transparent.

How do you display something in tkinter?

To display data we have to use insert() function. insert function takes two parameters. In our program, we have used entry widgets to capture the information provided by the user, and then we have to frame a sentence & that sentence is displayed in another Entry widget.


2 Answers

As mentioned in a comment by @CommonSense, you can toggle hide/show canvas items using:

canvas.itemconfigure(id, state='hidden'/'normal')

from a comment on an answer to this question, you get a similar functionality for widgets positioned with the grid geometry manager:

grid_remove is another option. It's advantage over grid_forget is that grid will remember all of the options, so that a simple grid() will put it right back. There is no pack_remove

like image 120
Reblochon Masque Avatar answered Oct 23 '22 15:10

Reblochon Masque


You need to use:

canvas.itemconfigure(id, state='hidden'/'normal')

as mentioned in @Reblochon Masque answer although attention to the id. this id is the return value of the widget placement method

id = parent.create_window(x, y, window=my_widget_name)

not the widget's name

like image 42
Kostas Markakis Avatar answered Oct 23 '22 17:10

Kostas Markakis