Как очистить окно в tkinter
Перейти к содержимому

Как очистить окно в tkinter

  • автор:

Как очистить окно после нажатия кнопки в Python Tkinter?

В настоящее время я создаю математическую викторину для детей в python tkinter. В этом опросе у меня есть 3 разных «страницы», как говорят. Стартовая страница, страница опроса и страница оценки, когда завершена опрос. На моей стартовой странице у меня есть три разных сложности викторины, которые пользователь может выбрать. Как я по существу очищать элементы окна от начальной страницы, такие как моя метка и кнопка, когда нажата кнопка «EASY» или «HARD», чтобы начать викторину?

How to clear out a frame in the Tkinter?

Tkinter frames are used to group and organize too many widgets in an aesthetic way. A frame component can contain Button widgets, Entry Widgets, Labels, ScrollBars, and other widgets.

If we want to clear the frame content or delete all the widgets inside the frame, we can use the destroy() method. This method can be invoked by targeting the children of the frame using winfo_children().

Example

Output

Running the above code will display a window containing a button “Clear” that targets all the widgets inside the frame and clears it.

Now click on the “Clear” Button and it will clear all the widgets inside the frame.

Как очистить окно в tkinter

Tkinter is a Python Package for creating effective GUI applications. Tkinter’s Canvas widget is nothing but a rectangular area that is used for drawing pictures, simple shapes, or any complex graph. We can place any widgets like text, button, or frames on the canvas.

The task here is to generate a Python script that can clear Tkinter Canvas. For that delete function of this module will be employed. This method has a special parameter all which represents all the component on the canvas. To clear this canvas give this special parameter to the delete method. Thus, the line below is sufficient to clear the canvas:

If you want to delete any specific item then you can assign a tag to that item and instead of all pass that tag to the delete method.

Python Tkinter clearing a frame

but frame.destroy() will totally remove the frame. And the other two also could not give me the result i want.What i need is just to clear up every items in the frame but the frame itself will stay. Is there anyway to do it?

Chris Aung's user avatar

3 Answers 3

w.winfo_children()
Returns a list of all w’s children, in their stacking order from lowest (bottom) to highest (top).

Will destroy all the widget in your frame. No need for a second frame.

pack_forget and grid_forget will only remove widgets from view, it doesn’t destroy them. If you don’t plan on re-using the widgets, your only real choice is to destroy them with the destroy method.

To do that you have two choices: destroy each one individually, or destroy the frame which will cause all of its children to be destroyed. The latter is generally the easiest and most effective.

Since you claim you don’t want to destroy the container frame, create a secondary frame. Have this secondary frame be the container for all the widgets you want to delete, and then put this one frame inside the parent you do not want to destroy. Then, it’s just a matter of destroying this one frame and all of the interior widgets will be destroyed along with it.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *