本文介绍了隐藏/显示画布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何隐藏画布,使其仅在您希望显示时显示?

How do you hide a canvas so it only shows when you want it displayed?

self.canvas.config(state='hidden')

只是给出错误说你只能使用禁用"或正常"

just gives the error saying you can only use 'disabled' or 'normal'

推荐答案

在评论中你说你正在使用 pack.在这种情况下,您可以使用 pack_forget 将其隐藏.

In the comments you say you are using pack. In that case, you can make it hidden by using pack_forget.

import tkinter as tk

def show():
    canvas.pack()

def hide():
    canvas.pack_forget()

root = tk.Tk()
root.geometry("400x400")

show_button = tk.Button(root, text="show", command=show)
hide_button = tk.Button(root, text="hide", command=hide)

canvas = tk.Canvas(root, background="pink")
show_button.pack(side="top")
hide_button.pack(side="top")
canvas.pack(side="top")

root.mainloop()

然而,在这种情况下通常最好使用 grid.pack_forget() 不记得小部件在哪里,所以下次你调用 pack 时,小部件可能会在不同的地方结束.要查看示例,请在 show_button.pack(side="top")

However, it's usually better to use grid in such a case. pack_forget() doesn't remember where the widget was, so the next time you call pack the widget may end up in a different place. To see an example, move canvas.pack(side="top") up two lines, before show_button.pack(side="top")

grid 有一个 grid_remove 方法,它会记住所有设置,以便后续调用 grid() 没有选项会将小部件放回完全相同的位置.

grid, on the other hand, has a grid_remove method which will remember all of the settings, so that a subsequent call to grid() with no options will put the widget back in the exact same spot.

这篇关于隐藏/显示画布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 16:24