【问题标题】:Destroying widgets from a different subroutine in tkinter从 tkinter 中的不同子例程销毁小部件
【发布时间】:2016-06-30 00:41:58
【问题描述】:

所以我现在使用 .place 来设置小部件的位置。

def printresults():
    SClabelspare=Label(cwindow, text ="Please enter the Customers ID Number:" ).place(x=10,y=560)

我正在寻找调用另一个将销毁这些小部件的子例程。我相信有一种叫做 .destroy() 或 .place_destroy 的东西?不过我不太确定这些是如何工作的,我试图创建一个看起来像这样的:

def destroy_widgets():
    SClabelspare.destroy()

但它只会产生一个错误代码,上面写着NameError: global name 'SClabelspare' is not defined

任何帮助将不胜感激!

【问题讨论】:

  • 不要为格式道歉,修复格式。

标签: python tkinter widget destroy


【解决方案1】:

或者,如果这假设非常简单,没有很多难以管理的全局变量,并且如果类结构只会引入不必要的复杂性,您可以尝试这样的事情(它在命令行的 python3 解释器中为我工作):

from tkinter import *
root = Tk()

def victim():
    global vic
    vic = Toplevel(root)
    vicblab = Label(vic, text='Please bump me off')
    vicblab.grid()

def bumper():
    global vic
    bump = Toplevel(root)
    bumpbutt = Button(bump, text='Bump off', command=vic.destroy)
    bumpbutt.grid()

victim()
bumper()

【讨论】:

    【解决方案2】:

    首先,place() 返回 None 所以 SClabelspare==None 不是 Tkinter ID。其次它是本地的,当函数退出时垃圾收集也是如此。您必须保留对可以通过多种方式完成的对象的引用。 Python 教程是一个好主意,可以在进一步学习之前获得基础知识https://wiki.python.org/moin/BeginnersGuide/Programmers 此外,在不使用类结构的情况下编写 Tkinter 应用程序是一种令人沮丧的体验,除非它非常简单。否则,您会遇到类似的错误,并且必须花费大量时间和精力来克服它们。这是我已有的示例,旨在对流程进行大致了解。

    from Tkinter import *
    from functools import partial
    
    class ButtonsTest:
       def __init__(self):
          self.top = Tk()
          self.top.title("Click a button to remove")
          Label(self.top, text="Click a button to remove it",
                bg="lightyellow").grid(row=0)
    
          self.top_frame = Frame(self.top, width =400, height=400)
          self.button_dic = {}
          self.buttons()
          self.top_frame.grid(row=1, column=0)
    
          Button(self.top_frame, text='Exit', bg="orange",
                 command=self.top.quit).grid(row=10,column=0, columnspan=5)
    
          self.top.mainloop()
    
       ##-------------------------------------------------------------------         
       def buttons(self):
          b_row=1
          b_col=0
          for but_num in range(1, 11):
             ## create a button and send the button's number to
             ## self.cb_handler when the button is pressed
             b = Button(self.top_frame, text = str(but_num), 
                        command=partial(self.cb_handler, but_num))
             b.grid(row=b_row, column=b_col)
             ## dictionary key=button number --> button instance
             self.button_dic[but_num] = b
    
             b_col += 1
             if b_col > 4:
                b_col = 0
                b_row += 1
    
       ##----------------------------------------------------------------
       def cb_handler( self, cb_number ):
          print "\ncb_handler", cb_number
          self.button_dic[cb_number].grid_forget()
    
    ##===================================================================
    BT=ButtonsTest()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-22
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      • 2018-07-05
      相关资源
      最近更新 更多