以下代码将在按下白色按钮、红色按钮和蓝色按钮时分别打开一个新窗口,每个按钮都向左对齐。在每个按钮的方法中,还有一个名为“关闭窗口”的附加按钮,用于关闭每次点击按钮时打开的新窗口。请保留HTML标签。
from Tkinter import*
import Tkinter as tk
class Packbox(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
whitebutton = Button(self, text="Red", fg="red", command=self.white_button)
whitebutton.pack( side = LEFT)
redbutton = Button(self, text="white", fg="white", command=self.red_button)
redbutton.pack( side = LEFT )
bluebutton = Button(self, text="Blue", fg="blue", command=self.blue_button)
bluebutton.pack( side = LEFT )
self.white_button()
self.red_button()
self.blue_button()
def white_button(self):
self.top = tk.Toplevel(self)
button = tk.Button(self.top, text="Close window", command=self.top.destroy)
label = tk.Label(self.top, wraplength=200,text="This prints white button txt")
label.pack(fill="x")
button.pack()
def red_button(self):
self.top = tk.Toplevel(self)
button = tk.Button(self.top, text="Close window", command=self.top.destroy)
label = tk.Label(self.top, wraplength=200,text="This prints red button txt")
label.pack(fill="x")
button.pack()
def blue_button(self):
self.top = tk.Toplevel(self)
button = tk.Button(self.top, text="Close window", command=self.top.destroy)
label = tk.Label(self.top, wraplength=200,text="This prints blue button txt")
label.pack(fill="x")
button.pack()
if __name__ == "__main__":
root = tk.Tk()
Packbox(root).pack(side="top", fill="both", expand=True)
root.mainloop()