如何在 Tkinter 中并排放置 window 和顶层?
How to put a window and a toplevel side by side in Tkinter?
from tkinter import *
def graphical_grid_init():
root = Tk()
root.title("2048")
w = Toplevel(root)
w.title("2048")
root.mainloop()
graphical_grid_init()
此代码产生 2 windows 但我希望它们并排放置,显然我不能在顶层 window 之后调用函数“网格”(例如:w.grid(column=1)
) 放置它,因为它不是小部件,但是在我们的主题中,他们要求我们这样做。
我怎样才能将 windows 并排放置?
谢谢
正如酷云所说,
w.geometry(f'+{px}+{px}') is what you need to use, keep in mind, you
will have to come up with something dynamic, because your pixels wont
be same as someone elses.
所以这是一个解决方案
from tkinter import *
def graphical_grid_init():
root = Tk()
root.title("2048")
width,height = 200,100 # with and height of the root window
w = Toplevel(root)
w.title("2048")
w_width,w_height = 200,100 # width and height of the toplevel window
setwinwidth = ((root.winfo_screenwidth()-width)//2) - width//2
setwinheight = (root.winfo_screenheight()-height)//2
settopwidth = ((root.winfo_screenwidth()-w_width)//2) + w_width//2
settopheight = (root.winfo_screenheight()-w_height)//2
root.geometry(f"{width}x{height}+{setwinwidth}+{setwinheight}")
w.geometry(f"{w_width}x{w_height}+{settopwidth}+{settopheight}")
root.mainloop()
graphical_grid_init()
在这里,我使用方法 winfo_screenwidth
和 winfo_screenheight
获取用户屏幕的实际宽度和高度。然后我使用这些尺寸以及为要显示的两个 windows 提供的尺寸,动态调整 windows 在屏幕上的大小和位置。
from tkinter import *
def graphical_grid_init():
root = Tk()
root.title("2048")
w = Toplevel(root)
w.title("2048")
root.mainloop()
graphical_grid_init()
此代码产生 2 windows 但我希望它们并排放置,显然我不能在顶层 window 之后调用函数“网格”(例如:w.grid(column=1)
) 放置它,因为它不是小部件,但是在我们的主题中,他们要求我们这样做。
我怎样才能将 windows 并排放置?
谢谢
正如酷云所说,
w.geometry(f'+{px}+{px}') is what you need to use, keep in mind, you will have to come up with something dynamic, because your pixels wont be same as someone elses.
所以这是一个解决方案
from tkinter import *
def graphical_grid_init():
root = Tk()
root.title("2048")
width,height = 200,100 # with and height of the root window
w = Toplevel(root)
w.title("2048")
w_width,w_height = 200,100 # width and height of the toplevel window
setwinwidth = ((root.winfo_screenwidth()-width)//2) - width//2
setwinheight = (root.winfo_screenheight()-height)//2
settopwidth = ((root.winfo_screenwidth()-w_width)//2) + w_width//2
settopheight = (root.winfo_screenheight()-w_height)//2
root.geometry(f"{width}x{height}+{setwinwidth}+{setwinheight}")
w.geometry(f"{w_width}x{w_height}+{settopwidth}+{settopheight}")
root.mainloop()
graphical_grid_init()
在这里,我使用方法 winfo_screenwidth
和 winfo_screenheight
获取用户屏幕的实际宽度和高度。然后我使用这些尺寸以及为要显示的两个 windows 提供的尺寸,动态调整 windows 在屏幕上的大小和位置。