如何在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个窗口,但我希望它们并排放置,显然我无法在顶层窗口(例如:w.grid(column=1))后调用功能“ grid”以将其原样放置不是小部件,但是在我们的主题中,他们要求我们这样做。
我如何并排放置窗户? 谢谢

解决方法

正如酷云所说,

w.geometry(f'+ {px} + {px}')是您需要使用的,请记住,您 将不得不想出动态的东西,因为您的像素不会 和别人一样。

这是一个解决方案

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_screenwidthwinfo_screenheight方法获得用户屏幕的实际宽度和高度。然后,我将使用这些大小以及为要显示的两个窗口提供的大小,以动态调整窗口的大小并将其放置在屏幕上。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...