如何破坏这个tkinter窗口

时间:2016-03-04 09:28:55

标签: python 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

2 个答案:

答案 0 :(得分:1)

mainloop调用后,tkinter程序不会执行代码。顾名思义,它是一个循环,程序保持在循环内,直到通过调用root.quit.终止循环。您需要构造一个事件驱动的程序并退出以响应用户操作。例如:http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/minimal-app.html

答案 1 :(得分:0)

app没有方法destroy,它是None对象,因为您的函数没有返回值,您应该将root.destroy放在win()内的按钮内{{1}功能。如:

import Tkinter
root = Tkinter.Tk()
button = Button(root, text="quit", command=root.destroy)
button.grid()
root.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()