无法关闭活动列表框窗口

时间:2018-04-14 14:44:41

标签: python class tkinter destroy

我在btnExit之外创建了一个函数class myWindow,并在类中创建了一个具有相同名称的方法。我已经通过多种方式调用了这些方法,并提出了许多建议来关闭当前窗口。什么都行不通。 从我的代码中可以明显看出,我只是学习Python并且来自c ++。

两个问题:

  1. 如果我使用command=self.btnExit,则会收到此错误:object has no attribute 'btnExit'
  2. 我可以调用command=btnExit访问该函数,但我尝试了九个函数关闭窗口。

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

    #!/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()
    

0 个答案:

没有答案