簡體   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