Python 无法关闭活动列表框窗口

Python 无法关闭活动列表框窗口,python,class,tkinter,destroy,Python,Class,Tkinter,Destroy,我在myWindow类的外部创建了一个函数btnExit,在类的内部创建了一个同名的方法。我以各种方式对每一个问题进行了讨论,并提出了关闭当前窗口的一些建议。什么都不管用。 从我的代码中我可以很清楚地看到Python,来自C++。 两个问题: 如果使用command=self.btnExit,则会出现以下错误:对象没有属性“btnExit” 我可以调用command=btnExit访问该函数,但我尝试关闭窗口中的九个函数 self.btn2 = tk.Button(self, text="EXI

我在myWindow类的外部创建了一个函数
btnExit
,在类的内部创建了一个同名的方法。我以各种方式对每一个问题进行了讨论,并提出了关闭当前窗口的一些建议。什么都不管用。 从我的代码中我可以很清楚地看到Python,来自C++。 两个问题:

  • 如果使用
    command=self.btnExit
    ,则会出现以下错误:
    对象没有属性“btnExit”
  • 我可以调用
    command=btnExit
    访问该函数,但我尝试关闭窗口中的九个函数

    self.btn2 = tk.Button(self, text="EXIT", fg="red", command=btnExit)
    
  • 最小功能代码:

    #!/usr/bin/python
    #Cura json iteration reporter
    
    import os
    import sys
    from tkinter import *
    import tkinter as tk
    root = Tk()
    root.overrideredirect(1)        #disable root window
    root.withdraw()
    
    #test data
    display = [
    "   0   .  name = Extruder",
    "   1   .  version = 2",
    "   2   .  metadata",
    "   3   .  .  type = extruder",
    "   4   .  .  author = Ultimaker",
    "   5   .  .  manufacturer = Unknown",
    "   6   .  .  setting_version = 1",
    "   7   .  .  visible = False",
    "   8   .  .  position = 0",
    "   9   .  settings",
    "  10   .  .  machine_settings"
    ]
    line_cnt = 10
    pathFilenameExt = "D:/Python/$my Projects/CURA json profiles examples/fdmextruder.def.json"
    fileDialogTitle = "Cura profile files"
    win_size = '500x500'
    
        # !!!! - this btnExit outside of any class class
    def btnExit():
        choice = messagebox.askquestion("Exit Program?", "Are you sure?", icon='warning')
        if choice == 'yes':
            #???what goes here to close current window????
            myWindow.destroy()
    
    class myWindow(tk.Frame):
        def __init__(self, parent):
            tk.Frame.__init__(self, parent)
            self.parent = parent
            display = []         #make sure display list is empty
    
        # !!!! - this btnExit is in the class whose window I want to close
        def btnExit(self):
            choice = messagebox.askquestion("Exit Program?", "Are you sure?", icon='warning')
            if choice == 'yes':
                #???what goes here to close current window????
                self.parent.deiconify()
                self.top.destroy()
    
        #now that we have a dictionary, create a readable formatted list and display it.
        # use current filename  NOTE: l1_text = StringVar() must be declared at top as global to be changed???
        def displysList(self):
            self.tk = Tk()        #creates a NEW window each time
            self.label1 = tk.Label(self, text = pathFilenameExt)
            self.label1.pack(side=TOP, anchor=W, fill=X, expand=YES)
            self.btn1 = tk.Button(self, text="New File", fg="blue", command=main)
            self.btn1.pack(side=TOP, anchor=W, fill=X, expand=YES)
            self.btn2 = tk.Button(self, text="EXIT", fg="red", command=btnExit)
            self.btn2.pack(side=TOP, anchor=W, fill=X, expand=YES)
    
            self.title('CURA json Lister')
            self.resizable(width=True, height=True)
            self.geometry(win_size)
            #create scroll bar for listbox
            self.scrollbary = tk.Scrollbar(self, orient='vertical')
            self.scrollbary.pack(side = 'right', fill = 'y' )
            self.scrollbarx = tk.Scrollbar(self, orient='horizontal')
            self.scrollbarx.pack(side = 'bottom', fill = 'x' )
    
            height =400     #in characters wider and taller than the screen to use all space available
            width = 400
            #list box create
            self.list2Box=tk.Listbox(self, selectmode=EXTENDED)
            self.list2Box.config(height=height, width=width)
            self.scrollbary.config(command = self.list2Box.yview )
            self.scrollbarx.config(command = self.list2Box.xview )
    
            i = 0
            while i < line_cnt:
                self.list2Box.insert(END,display[i])
                i += 1
            self.list2Box.pack(side='left')
    
    def main():
        #no other setup needed here for now
        windA = myWindow            #CREATE AN INSTANCE TO CALL ppd
        windA.displysList(root)   #redisplay each new file
    #end main
    
    if __name__ == '__main__':
        main()
        root.mainloop()
    
    #/usr/bin/python
    #库拉json迭代报告器
    导入操作系统
    导入系统
    从tkinter进口*
    将tkinter作为tk导入
    root=Tk()
    root.overrideredirect(1)#禁用根窗口
    root.draw()
    #测试数据
    显示=[
    “0.名称=挤出机”,
    “1.版本=2”,
    “2.元数据”,
    “3.类型=挤出机”,
    “4.作者=Ultimaker”,
    “5.制造商=未知”,
    “6.设置_version=1”,
    “7.可见=假”,
    “8..位置=0”,
    “9.设置”,
    “10.机器设置”
    ]
    线_cnt=10
    pathFilenameExt=“D:/Python/$my Projects/CURA json配置文件示例/fdmextruder.def.json”
    fileDialogTitle=“Cura配置文件”
    win_尺寸='500x500'
    # !!!! - 此btnextit位于任何类之外
    def btnExit():
    choice=messagebox.askquestion(“退出程序?”,“确定吗?”,icon='warning')
    如果选项==“是”:
    #??这里是什么来关闭当前窗口的????
    myWindow.destroy()
    类myWindow(tk.Frame):
    定义初始化(自身,父级):
    tk.Frame.\uuuu init\uuuuu(自,父)
    self.parent=parent
    显示=[]#确保显示列表为空
    # !!!! - 此btnExit位于我要关闭其窗口的类中
    def btnExit(自身):
    choice=messagebox.askquestion(“退出程序?”,“确定吗?”,icon='warning')
    如果选项==“是”:
    #??这里是什么来关闭当前窗口的????
    self.parent.deiconify()
    self.top.destroy()
    #现在我们有了一个字典,创建一个可读的格式化列表并显示它。
    #使用当前文件名注意:l1_text=StringVar()必须在顶部声明为全局文件名才能更改???
    def显示列表(自我):
    self.tk=tk()#每次创建一个新窗口
    self.label1=tk.Label(self,text=pathFilenameExt)
    self.label1.pack(side=TOP,anchor=W,fill=X,expand=YES)
    self.btn1=tk.Button(self,text=“新建文件”,fg=“蓝色”,command=main)
    self.btn1.pack(侧=顶部,锚=W,填充=X,扩展=是)
    self.btn2=tk.按钮(self,text=“退出”,fg=“红色”,command=btnExit)
    self.btn2.pack(侧=顶部,锚=W,填充=X,扩展=是)
    self.title('CURA json Lister')
    可自行调整大小(宽度=真,高度=真)
    自几何(win_尺寸)
    #为列表框创建滚动条
    self.scrollbary=tk.Scrollbar(self,orient='vertical')
    self.scrollbary.pack(side='right',fill='y')
    self.scrollbarx=tk.Scrollbar(self,orient='horizontal')
    self.scrollbarx.pack(side='bottom',fill='x')
    高度=400#比屏幕宽和高的字符,以使用所有可用空间
    宽度=400
    #列表框创建
    self.list2Box=tk.Listbox(self,selectmode=EXTENDED)
    self.list2Box.config(高度=高度,宽度=宽度)
    self.scrollbary.config(命令=self.list2Box.yview)
    self.scrollbarx.config(命令=self.list2Box.xview)
    i=0
    当我
    您发布了许多与所问问题无关的代码。请尽量减少到a。你的代码有很多问题。对于初学者来说,要创建myWindow类的实例,需要调用它并传递它需要的任何参数。i、 e.在
    main()
    中,它应该是
    windA=myWindow(root)
    。还要注意,通常在基于
    tkinter
    的程序中,不能多次调用
    Tk()。要创建新窗口,请使用
    tk.Toplevel()
    。这里有一点关于它。