【发布时间】:2019-04-14 19:57:03
【问题描述】:
我想隐藏一个 tkinter 按钮,但不是在用户单击它时。我只是想随机隐藏它。我将如何在 Python 中做到这一点?下面是我试过的代码:
self.startGame = Button(self.canvas, text="Start", background='white', command = self.startGame, font=("Helvetica"))
self.startGame.place(x=770, y=400)
隐藏它:
self.startGame.bind('<Button-1>', self.hide_me)
def hide_me(self, event):
print('hide me')
event.widget.pack_forget()
它甚至没有进入 hide_me 函数。
【问题讨论】:
-
.pack_forget()不会对.pack()最初不可见的小部件做任何事情。您需要改用.place_forget()。 -
您的按钮名称 self.startGame 并且它链接到一个名为 self.startGame 的函数。这可能就是为什么它没有进入 hide_me。上面 jasonharper 的回答解释了 hide_me 一旦你到达它就不会做的事情。
-
@DaveMaheux 命令只是链接回按钮,这实际上并没有阻止
bind()激活hid_me。当他们将pack_forget()更改为place_forget()时,此代码将起作用。但是我同意command=self.startGame在这里是错误的。
标签: python python-3.x user-interface button tkinter