1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| from tkinter import * import tkinter.messagebox as messagebox
window = Tk() window.title("主窗体") window.geometry("500x250") window.resizable(0, 0)
def create_toplevel(): toplevel = Toplevel() toplevel.title("子窗体1") toplevel.geometry("300x100") toplevel.resizable(0, 0) Label( toplevel, text="https://stitch-top.github.io/", bg="#9BCD9B", font=("宋体", 11) ).pack()
top = Toplevel(window) top.title("子窗体2") top.geometry("300x100")
def hide_window(): top.iconify()
def withdraw_window(): top.withdraw()
def show_window(): top.deiconify()
def get_code(): code = top.frame() messagebox.showinfo("窗口识别码", f"窗口识别码:{code}!")
def check_state(): state = top.state() messagebox.showinfo("窗口状态", f"当前窗口状态:{state}!")
def open_transient_window(): transient_window = Toplevel(window) transient_window.title("子窗体3") transient_window.geometry("300x100") transient_window.transient(window) transient_window.protocol( "WM_DELETE_WINDOW", transient_window.destroy )
def create_group_window(): group_window1 = Toplevel(top) group_window1.title("组窗口1") group_window1.geometry("260x100") group_window1.group(top)
def create_another_group_window(): group_window2 = Toplevel(top) group_window2.title("组窗口2") group_window2.geometry("260x100") group_window2.group(top)
Button( window, text="创建窗口1", width=20, height=1, command=create_toplevel ).pack() Button( window, text="最小化窗口2", width=20, height=1, command=hide_window ).pack() Button( window, text="移除窗口2", width=20, height=1, command=withdraw_window ).pack() Button( window, text="显示窗口2", width=20, height=1, command=show_window ).pack() Button( window, text="获取窗口2的识别码", width=20, height=1, command=get_code ).pack() Button( window, text="检查窗口2的状态", width=20, height=1, command=check_state ).pack() Button( window, text="打开窗口3", width=20, height=1, command=open_transient_window ).pack()
Button( top, text="创建组窗口1", width=10, height=1, command=create_group_window ).pack() Button( top, text="创建组窗口2", width=10, height=1, command=create_another_group_window ).pack()
window.mainloop()
|