亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何創建一個新的 python tkinter 窗口,使其完全適合可用的桌面區域,但沒有最大化?

如何創建一個新的 python tkinter 窗口,使其完全適合可用的桌面區域,但沒有最大化?

呼如林 2022-05-19 18:40:02
我的問題是使用 .geometry 設置高度和寬度設置窗口內空間的高度和寬度 - 標題欄和窗口邊框使其更大。以下是我到目前為止的代碼。正如您將看到的,即使我讓它創建了一個與可用屏幕區域精確大小的窗口,窗口最終還是太大了,因為在設置大小時沒有考慮標題欄和邊框。看到“這里需要改變什么”部分了嗎?這兩個語句需要更改或替換為什么,很簡單?(如果你做對了,窗口應該完全適合桌面上的可用空間,標題欄和邊框清晰可見。)請保持簡單 - 我對這一切還是陌生的。:-)#Important variables:#screenAvailableWidth, screenAvailableHeight: Height & width of#                                              screen without#                                              taskbar#windowWidth, windowHeight: Height & width of new window to create#window: Object of class Tk() from tkinter.#Get screen height and width WITHOUT including the taskbar.#This part works fine - I've tested the result and it's good.from win32api import GetMonitorInfo, MonitorFromPointmonitorInfo = GetMonitorInfo(MonitorFromPoint((0,0)))workArea = monitorInfo.get("Work")screenAvailableWidth = workArea[2]screenAvailableHeight = workArea[3]#Create a tkinter windowfrom tkinter import Tk window = Tk()#Set new window height & width#--------------------------------------------#----- HERE. What needs to change here? -----#--------------------------------------------windowWidth = screenAvailableWidthwindowHeight = screenAvailableHeight#--------------------------------------------#Show debug infoprint("")print("screenAvailableWidth:",screenAvailableWidth,      " screenAvailableHeight:",screenAvailableHeight)print("windowWidth:\t",windowWidth," windowHeight:\t",windowHeight)#Set the new window to upper left corner and height &# width of screenwindow.geometry("{}x{}+0+0".format(windowWidth,windowHeight))#Show the windowwindow.mainloop()
查看完整描述

2 回答

?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

monitorInfo您可以使用winfo_screedwidth()and代替使用winfo_screenheight()。


這是如何做到的:


windowWidth = window.winfo_screenwidth

windowHeight = window.winfo_screenheight


window.geometry("%sx%s" %(screenWidth, screenHeight)

您可以使用window.overrideredirect(True)來擺脫任務欄。它還將擺脫頂部的欄,因此您必須使用 alt+f4 退出窗口。


此外,這些選項是可選的,但通過消除使用代碼執行某些任務時可能發生的錯誤,肯定會使您的代碼更加清晰和高效。


——如果你愿意,你可以停止閱讀——


而不是from tkinter import Tk使用import tkinter as tk. 這意味著您可以編寫tk.widget,而不必寫出您使用的每個小部件。此外,如果您使用,from tkinter import *您可以使用import tkinter as tk,因為它將所有屬性分組到tk. 它肯定會清理屬性,并且您不會將它們與所有內置屬性一起使用


此外,將所有小部件放入class. 你可以這樣做:


class Name(tk.Frame):

在里面你需要編寫__init__函數:


def __init__(self, master, **kwargs): #Feel free to add any extra parameters

在__init__您編寫的函數內部:


super().__init__(master, **kwargs)

該類使您的代碼整潔,而每當您創建函數時都需要__init__and 。super()


此外,您可以在__name__ == "__main__"將代碼導入另一個腳本時使用 if 條件來阻止代碼運行。


這是如何做到這一點:


def func_name():

    root = tk.Tk()

    #Add any root titles, geometry or any other configurations here

    app = Window(root) #Instead of Window, replace it with your class name

    app.pack(fill=tk.BOTH, expand=True)

    #Add any app configurations here

    root.mainloop()


if __name__ == "__main__":

    func_name()

您可以包含所有這些功能以使您的代碼更整潔。


查看完整回答
反對 回復 2022-05-19
?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

我得到了它!訣竅是只要窗口可見(即使它在屏幕外的某個地方),您就可以這樣做:


titlebarHeight = window.winfo_rooty() - window.winfo_y()

borderSize= window.winfo_rootx() - window.winfo_x()

一旦你有了這些,你可以調整你想要的窗口寬度和高度來糾正標題欄和邊框,如下所示:


WindowWidth = WindowWidth - (borderSize * 2)

WindowHeight = (WindowHeight - titlebarHeight) - borderSize

因此,最終運行的代碼是這樣的(它是完整的 - 將其復制并粘貼到您選擇的編輯器中,它應該按原樣運行):


#Get screen height and width WITHOUT including the taskbar.

#This part works fine - I've tested the result and it's good.

from win32api import GetMonitorInfo, MonitorFromPoint

monitorInfo = GetMonitorInfo(MonitorFromPoint((0,0)))

workArea = monitorInfo.get("Work")

screenAvailableWidth = workArea[2]

screenAvailableHeight = workArea[3]


#Create a tkinter window

from tkinter import Tk 

window = Tk()


#Set new window height & width

#--------------------------------------------

#-----   HERE. This is what changed:    -----

#--------------------------------------------

#Make window visible so we can get some geometry

window.update_idletasks()

#Calculate title bar and border size

titlebarHeight = window.winfo_rooty() - window.winfo_y()

borderSize= window.winfo_rootx() - window.winfo_x()


#Start with full available screen

windowWidth = screenAvailableWidth

windowHeight = screenAvailableHeight


#Adjust for title bar and borders

windowWidth = windowWidth - (borderSize * 2 )

windowHeight = (windowHeight - titlebarHeight) - borderSize

#--------------------------------------------


#Show debug info

print("")

print("screenAvailableWidth:",screenAvailableWidth,

      " screenAvailableHeight:",screenAvailableHeight)

print("windowWidth:\t",windowWidth," windowHeight:\t",windowHeight)


#Set the new window to upper left corner and height &

# width of screen (after adjustment)

window.geometry("{}x{}+0+0".format(windowWidth,windowHeight))


#Show the window

window.mainloop()

(突破是在此處檢查可疑重復問題(及其評論和回復)中提到的所有屬性:tkinter window get x, y, geometry/coordinates without top of window 該問題并沒有真正將各個部分放在一個新手中-友好的方式,也沒有任何可用的示例代碼,因此希望將來對其他人有用。)


對于所有為我發表評論并提供解決方案的人,謝謝!我真的很感謝你花時間這樣做。:-)


查看完整回答
反對 回復 2022-05-19
  • 2 回答
  • 0 關注
  • 226 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號