【问题标题】:Is there a way to hide all existing buttons?有没有办法隐藏所有现有的按钮?
【发布时间】:2021-06-02 08:30:38
【问题描述】:

我正在研究 python,只是在做一些小游戏,但我被困住了,因为我找不到关于这个主题的任何信息。

我有 2 个按钮,并且希望在两个按钮中的任何一个单击时都隐藏它们。有没有办法做到这一点?

这是我在 RedRidingHood 类中的按钮代码:

def p1Choice_a(self, event):
    print("You choose to run from the big wolf")
    event.widget.pack_forget()

def p1Choice_b(self, event):
    print("You choose to talk the big wolf")
    event.widget.pack_forget()

这里也是类RedRidingHood中按钮的主要启动和创建:

def start(self):
    self.create_button_p1()

def create_button_p1(self, position):
    p1ca = Button(frame, text="A. Run from the big wolf")
    p1ca.bind('<Button-1>', self.p1Choice_a)
    p1ca.pack( side = LEFT )

    p1cb = Button(frame, text="B. Have a conversation with the big wolf")
    p1cb.bind('<Button-2>', self.p1Choice_b)
    p1cb.pack( side = RIGHT )


rrhStart = RedRidingHood()
rrhStart.start()

【问题讨论】:

  • 您将按钮名称设为全局并相应隐藏
  • @Sujay 如果他们的名字变成全球性的,我该如何隐藏他们?
  • 检查答案并相应替换

标签: python tkinter button


【解决方案1】:

你可以替换函数

def create_button_p1(self, position):
    self.p1ca = Button(frame, text="A. Run from the big wolf")
    self.p1ca.bind('<Button-1>', self.p1Choice_a)
    self p1ca.pack( side = LEFT )

    self.p1cb = Button(frame, text="B. Have a conversation with the big wolf")
    self.p1cb.bind('<Button-2>', self.p1Choice_b)
    self.p1cb.pack( side = RIGHT )

然后是这些函数

def p1Choice_a(self, event):
    print("You choose to run from the big wolf")
    self.p1ca.pack_forget()
    self.p1cb.pack_forget()

def p1Choice_b(self, event):
    print("You choose to talk the big wolf")
    self.p1cb.pack_forget()
    self.p1ca.pack_forget()

【讨论】:

  • self.p1ca.pack_forget()self.p1cb.pack_forget() 都应该在 p1Choice_ap1Choice_b 内。它有效!谢谢!
  • 是的,因为它是课程的一部分,并且可以在课程中使用
【解决方案2】:

使用

.pack_forget()
.grid_forget()
.place_forget()

取决于您使用的几何管理器。

【讨论】:

    猜你喜欢
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多