如何使窗口适合每种屏幕分辨率?

问题描述 投票:0回答:1

我编写了一个带有许多小部件的简单程序,但是当我将程序移动到较小的第二个显示器时,效果并不好。一半的小部件无法被看到。 =

我需要在每个屏幕上安装根。

python tkinter resolution
1个回答
0
投票

为了确保程序的 GUI(图形用户界面)适合任何屏幕,特别是在不同尺寸的显示器之间移动时,您可以在许多 GUI 框架中使用

geometry
方法。我将提供一个使用 Tkinter 库的 Python 通用示例,该库通常用于创建 GUI 应用程序。如果您使用不同的库,概念应该是相似的。

import tkinter as tk

class YourApp:
    def __init__(self, root):
        self.root = root
        self.initialize_widgets()

    def initialize_widgets(self):
        # Your widget creation code here

if __name__ == "__main__":
    root = tk.Tk()
    app = YourApp(root)

    # Get screen width and height
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()

    # Set the size and position of the root window
    root_width = 800  # set your desired width
    root_height = 600  # set your desired height

    # Calculate the position to center the window on the screen
    x_position = (screen_width - root_width) // 2
    y_position = (screen_height - root_height) // 2

    # Set the geometry of the root window
    root.geometry(f"{root_width}x{root_height}+{x_position}+{y_position}")

    root.mainloop()

此示例创建一个带有根窗口的 Tkinter 应用程序并初始化小部件。然后使用

geometry
方法设置根窗口的大小和位置。它会考虑窗口的大小来计算屏幕上的中心位置。

您可以调整

root_width
root_height
变量以适合您所需的尺寸。这有助于使窗口显示在屏幕中央并适合可见区域,即使在不同尺寸的显示器之间切换时也是如此。

© www.soinside.com 2019 - 2024. All rights reserved.