2016-03-04 124 views
-1

请问我该如何销毁这个tkinter窗口?如何销毁这个tkinter窗口

from Tkinter import * 
import time 

def win(): 
    root = Tk() 

    root.wait_visibility(root) 
    root.attributes('-alpha', 0.7) 

    root.overrideredirect(True) 
    root.configure(background='black') 
    root.wm_attributes("-topmost", 1) #zostane navrchu 
    w = 200 # width for the Tk root 
    h = 50 # height for the Tk root 
    # get screen width and height 
    ws = root.winfo_screenwidth() # width of the screen 
    hs = root.winfo_screenheight() # height of the screen 
    # calculate x and y coordinates for the Tk root window 
    x = (ws/2) - (w/2) 
    y = hs - h - 50 #(hs/2) - (h/2) 

    lab = Label(root, text="Hello, world!") 
    lab.pack() 

    #x = 50 
    #y = 50 
    root.geometry('%dx%d+%d+%d' % (w, h, x, y)) 
    root.mainloop() 

app = win() 
app.destroy() #not working 

回答

0

app没有方法destroy,它是一个None对象,因为你的函数没有返回值,你应该把root.destroy一个按钮内部的win()函数内。如:

import Tkinter 
root = Tkinter.Tk() 
button = Button(root, text="quit", command=root.destroy) 
button.grid() 
root.mainloop() 

使用类:在mainloop电话后

import Tkinter 

class App(): 
    def __init__(self): 
     self.root = Tkinter.Tk() 
     button = Tkinter.Button(self.root, text = 'root quit', command=self.quit) 
     button.pack() 
     self.root.mainloop() 

    def quit(self): 
     self.root.destroy() 

app = App() 
+1

你是正确的,win()不返回一个值;不过,您建议的解决方案也不行。在调用root.quit之前,函数'mainloop'不会返回。 –

+0

是的,你是对的我正在编辑它,以显示他如何把一个按钮,并将其设置为销毁,我评论的链接实际上是哈哈 – Mixone