繁体   English   中英

如何让这个笔记本缩放到 tkinter 的屏幕分辨率?

[英]How can I make this notebook scale to the screen resolution in tkinter?

我目前正在 tkinter 中编写一个应用程序,我希望它可以在具有低端或旧硬件的不同类型的系统上使用。 为了让我自己更容易,我将只支持 720p 及以上的显示器。 该程序现在可以重新缩放,但笔记本元素(包含 10 页)给我带来了问题。 当程序启动时,它以 720p 规格运行,不可调整大小 window,并且有一个全屏按钮。

非全屏模式(720p 窗口)的笔记本代码:

itemsNotebook = ttk.Notebook(middleRight)
itemsNotebook.grid(row=0, column=0, padx = 20, pady = 10, sticky = N)

print(str(screenWidth) + "x" + str(screenHeight))

pageOne = Frame(itemsNotebook, width=652, height = 536, bg = "#333333")
pageTwo = Frame(itemsNotebook, width=652, height = 536, bg = "#333333")
pageThree = Frame(itemsNotebook, width=652, height = 536, bg = "#333333")
pageFour = Frame(itemsNotebook, width=652, height = 536, bg = "#333333")
pageFive = Frame(itemsNotebook, width=652, height = 536, bg = "#333333")
pageSix = Frame(itemsNotebook, width=652, height = 536, bg = "#333333")
pageSeven = Frame(itemsNotebook, width=652, height = 536, bg = "#333333")
pageEight = Frame(itemsNotebook, width=652, height = 536, bg = "#333333")
pageNine = Frame(itemsNotebook, width=652, height = 536, bg = "#333333")
pageTen = Frame(itemsNotebook, width=652, height = 536, bg = "#333333")

我了解这部分的作用,它使笔记本在 window 上具有一定的尺寸,而不是全屏模式。 我的程序有一个按钮可以进入全屏模式,并连接了一个 function。

全屏切换代码:

def screenModeToggle():
    global fullscreen
    global blockScreenAdjustment

    if screenWidth >= 1280 and screenHeight >= 720:
        fullscreen = not fullscreen
        mainWindow.attributes("-fullscreen", fullscreen)

    else:
        tkinter.messagebox.showwarning("screen size too small", "Your screen has to be at least 720p (1280x720) to enter fullscreen mode.")
        blockScreenAdjustment = True
        print("screen adjustment blocked!")

    if fullscreen == True and blockScreenAdjustment == False and screenWidth >= 1920 and screenHeight >= 1080:
        pageOne.config(width=math.floor((1078/1707)*screenWidth), height=math.floor((776/960)*screenHeight))
        pageTwo.config(width=math.floor((1078/1707)*screenWidth), height=math.floor((776/960)*screenHeight))
        pageThree.config(width=math.floor((1078/1707)*screenWidth), height=math.floor((776/960)*screenHeight))
        pageFour.config(width=math.floor((1078/1707)*screenWidth), height=math.floor((776/960)*screenHeight))
        pageFive.config(width=math.floor((1078/1707)*screenWidth), height=math.floor((776/960)*screenHeight))
        pageSix.config(width=math.floor((1078/1707)*screenWidth), height=math.floor((776/960)*screenHeight))
        pageSeven.config(width=math.floor((1078/1707)*screenWidth), height=math.floor((776/960)*screenHeight))
        pageEight.config(width=math.floor((1078/1707)*screenWidth), height=math.floor((776/960)*screenHeight))
        pageNine.config(width=math.floor((1078/1707)*screenWidth), height=math.floor((776/960)*screenHeight))
        pageTen.config(width=math.floor((1078/1707)*screenWidth), height=math.floor((776/960)*screenHeight))

    if fullscreen == False and blockScreenAdjustment == False:
        pageOne.config(width=652, height = 536)
        pageTwo.config(width=652, height = 536)
        pageThree.config(width=652, height = 536)
        pageFour.config(width=652, height = 536)
        pageFive.config(width=652, height = 536)
        pageSix.config(width=652, height = 536)
        pageSeven.config(width=652, height = 536)
        pageEight.config(width=652, height = 536)
        pageNine.config(width=652, height = 536)
        pageTen.config(width=652, height = 536)

我正在计算的数字是在我当前的显示配置上看起来不错的尺寸。 我认为将这些数字除以我的屏幕分辨率,然后将它们乘以程序运行的显示尺寸就可以了,但这不能正常工作。 笔记本对于高分辨率来说太小了,对于较小的分辨率来说太大了。 screenHeightscreenWidth是包含使用以下代码的屏幕高度和宽度的变量:

screenWidth = mainWindow.winfo_screenwidth()
screenHeight = mainWindow.winfo_screenheight()

有人可以帮我正确地缩放和调整笔记本的大小吗? 我这样做的方式感觉有点像一种解决方法,它甚至不能正常工作。

这是我第一次来这里,所以如果我忘记了一些重要信息,请告诉我。

作为一般经验法则,您不应该为Frame小部件指定宽度和高度。 相反,在适当的情况下为框架内的小部件指定大小(例如:对于TextCanvas小部件),然后让框架扩大或缩小以适应内容。

第二条经验法则是将您的 GUI 设计为在尽可能小的空间中工作,然后使用几何管理器( packplacegrid )选项来允许小部件扩展以填充它们的区域。

下面的示例创建了七个选项卡,每个选项卡都具有不同的背景颜色以用于说明目的。 window 最初设置为 800x600,但由于我们将笔记本添加到 window 的方式,选项卡全部展开以填充 window。

另请注意,当您手动调整 window 的大小时,笔记本电脑将自动扩展和收缩。 即使您强制 window 全屏显示,这也有效。

最后,请注意,即使我们创建了许多选项卡,因为每个选项卡实际上都是相同的,所以我们可以循环创建它们。 我们有一个名为pages的字典变量,而不是pageOnepageTwo等变量。 这极大地有助于减少重复代码。

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.geometry("800x600")
notebook = ttk.Notebook(root)
notebook.pack(fill="both", expand=True)

pages = {}
for color in ("white", "red", "orange", "green", "blue", "violet", "black"):
    page = tk.Frame(notebook, background=color)
    pages[color] = page
    notebook.add(page, text=color.title())

root.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM