【发布时间】:2014-11-12 20:37:25
【问题描述】:
代码如下:
from tkinter import *
class PopupTk(Frame):
def __init__(self, master=None, title="Notification", msg="New information", duration=2):
Frame.__init__(self, master)
self.duration = duration
close_button = Button(self, text="C", command=self.master.destroy)
close_button.pack(side=LEFT)
title_label = Label(self, text=title)
title_label.config(justify=LEFT)
title_label.pack()
msg_label = Label(self, text=msg)
msg_label.config(justify=LEFT)
msg_label.pack()
self.pack(side=TOP, fill=BOTH, expand=YES, padx=10, pady=10)
# get screen width and height
ws = self.master.winfo_screenwidth()
hs = self.master.winfo_screenheight()
w = 300
h = 100
# calculate position x, y
x = ws - w
y = hs - h
self.master.geometry('%dx%d+%d+%d' % (w, h, x, y))
self.master.overrideredirect(True)
self.master.lift()
def auto_close(self):
msec_until_close = self.duration * 1000
self.master.after(msec_until_close, self.master.destroy)
if __name__ == '__main__':
root = Tk()
sp = PopupTk(root, duration=3)
sp.auto_close()
root.call('wm', 'attributes', '.', '-topmost', True)
root.mainloop()
结果如下:
它仍然居中对齐(默认)。
我使用的是 python 3.4,并且已经在 osx 和 ubuntu 14.04 上进行了测试,顺便说一句。
【问题讨论】: