我有我喜欢的布局,但是当我移动根窗口时,我希望顶层窗口与根窗口一起移动。这可能吗?我还喜欢这样的想法:用户可以选择从根窗口移动一个或所有顶级窗口。
如果不可能,是否还有其他类似的选项?
from Tkinter import *
import sys
def main():
root = Tk()
root.title('skyreach')
root.geometry('1900x1000+0+0')
root.lower()
board = Toplevel(root)
board.geometry('1875x200+10+30')
board.title('Tasked')
board1 = Toplevel(root)
board1.geometry('1875x200+10+270')
board1.title('Sched and Confirm')
board2 = Toplevel(root)
board2.geometry('1875x200+10+510')
board2.title('Sched')
board3 = Toplevel(root)
board3.geometry('1875x200+10+750')
board3.title('Completed')
frame = Frame(root, relief = RAISED, borderwidth = 10)
frame.pack(fill = BOTH, expand = 1)
frame = Frame(board, relief = RAISED, borderwidth = 10)
frame.pack(side = BOTTOM, fill = BOTH, expand = 1)
frame = Frame(board1, relief = RAISED, borderwidth = 10)
frame.pack(side = BOTTOM, fill = BOTH, expand = 1)
frame = Frame(board2, relief = RAISED, borderwidth = 10)
frame.pack(side = BOTTOM, fill = BOTH, expand = 1)
frame = Frame(board3, relief = RAISED, borderwidth = 10)
frame.pack(side = BOTTOM, fill = BOTH, expand = 1)
export = Button(root, text = 'Export to CSV', width = 20)
export.pack(side = LEFT)
refresh = Button(root, text = 'Refresh', width = 20)
refresh.pack(side = LEFT)
exitbut = Button(root, text = 'Exit', width = 20, command = sys.exit)
exitbut.pack(side = LEFT)
root.mainloop()
if __name__== "__main__":
main()
在 Windows 7 上,<Configure>
当拖动窗口以及调整窗口大小时会触发该事件。当根窗口移动时,您可以使用此事件来移动顶级窗口。
示例:
from Tkinter import *
class App():
def __init__(self):
self.root = Tk()
self.root.geometry('300x300+100+100')
self.root.bind('<Configure>', self.root_conf)
self.top = []
self.top.append(Toplevel(self.root))
self.top[0].geometry('200x200+150+150')
self.top[0].transient(self.root)
self.x_off = []
self.y_off = []
self.x_off.append(50)
self.y_off.append(50)
self.top[0].bind('<Configure>', lambda e: self.top_conf(e, 0))
self.top.append(Toplevel(self.root))
self.top[1].geometry('200x200+450+150')
self.top[1].transient(self.root)
self.x_off.append(350)
self.y_off.append(50)
self.top[1].bind('<Configure>', lambda e: self.top_conf(e, 1))
self.root.mainloop()
def root_conf(self, e):
self.top[0].geometry('{}x{}+{}+{}'.format(self.top[0].winfo_width(), self.top[0].winfo_height(), e.x+self.x_off[0], e.y+self.y_off[0]))
self.top[1].geometry('{}x{}+{}+{}'.format(self.top[1].winfo_width(), self.top[1].winfo_height(), e.x+self.x_off[1], e.y+self.y_off[1]))
def top_conf(self, e, num):
self.x_off[num] = e.widget.winfo_x()-self.root.winfo_x()
self.y_off[num] = e.widget.winfo_y()-self.root.winfo_y()
if __name__== "__main__":
App()
<小时/>
请注意,根据 @7stud 的评论,这在 MacOSX 上不起作用,因为 <Configure>
拖动窗口时不会触发事件。
我是一名优秀的程序员,十分优秀!