我目前正在 tkinter 中撰寫一個應用程式,我希望它可以在具有低端或舊硬體的不同型別的系統上使用。為了方便我自己,我將只支持 720p 及以上的顯示幕。該程式現在可以重新縮放,但筆記本元素(包含 10 頁)給我帶來了問題。當程式啟動時,它作為一個 720p 規格的不可調整大小的視窗運行,并且它有一個全屏按鈕。
非全屏模式(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")
我明白這部分的作用,它使筆記本在視窗上有一定的大小,而不是在全屏模式下。我的程式有一個按鈕可以進入全屏模式,并連接了一個功能。
全屏切換代碼:
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)
我正在計算的數字是在我當前的顯示配置下看起來不錯的尺寸。我認為通過我的螢屏解析度來劃分這些數字,然后將它們乘以程式運行的顯示尺寸就可以了,但這不能正常作業。筆記本對于高解析度來說太小了,對于較小的解析度來說太大了。的screenHeight和screenWidth是含有高度和使用此代碼的螢屏的寬度的變數:
screenWidth = mainWindow.winfo_screenwidth()
screenHeight = mainWindow.winfo_screenheight()
有人可以幫助我正確調整筆記本的縮放比例和大小嗎?我這樣做的方式感覺有點像一種解決方法,它甚至無法正常作業。
這是我第一次來這里,所以如果我忘記了一些重要資訊,請告訴我。
uj5u.com熱心網友回復:
作為一般經驗法則,您不應為Frame小部件指定寬度和高度。相反,在適當的情況下為框架內的小部件指定大小(例如:forText和Canvas小部件),然后讓框架增長或縮小以適應內容。
第二條經驗法則是將 GUI 設計為在盡可能小的空間內作業,然后使用幾何管理器 ( pack, place, grid) 選項允許小部件擴展以填充其區域。
以下示例創建了七個選項卡,每個選項卡都具有不同的背景顏色以供說明。視窗最初設定為 800x600,但由于我們將筆記本添加到視窗的方式,選項卡都展開以填充視窗。
另請注意,當您手動調整視窗大小時,筆記本將自動擴展和收縮。即使您強制視窗全屏顯示,這也有效。
最后,請注意,即使我們創建了許多選項卡,因為每個選項卡幾乎完全相同,我們也可以在回圈中創建它們。我們有一個名為 的字典變數pageOne,而不是像、pageTwo等變數pages。這極大地有助于減少重復代碼。
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/398963.html
