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()
您可以包含所有這些功能以使您的代碼更整潔。

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 該問題并沒有真正將各個部分放在一個新手中-友好的方式,也沒有任何可用的示例代碼,因此希望將來對其他人有用。)
對于所有為我發表評論并提供解決方案的人,謝謝!我真的很感謝你花時間這樣做。:-)
添加回答
舉報