如何创建一个新的 python tkinter 窗口,使其完全适合可用的桌面区域,但没有最大化?

我的问题是使用 .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, 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. What needs to change here? -----

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

windowWidth = screenAvailableWidth

windowHeight = screenAvailableHeight

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


#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

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


#Show the window

window.mainloop()


呼如林
浏览 238回答 2
2回答

长风秋雁

monitorInfo您可以使用winfo_screedwidth()and代替使用winfo_screenheight()。这是如何做到的:windowWidth = window.winfo_screenwidthwindowHeight = window.winfo_screenheightwindow.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()您可以包含所有这些功能以使您的代码更整洁。

拉莫斯之舞

我得到了它!诀窍是只要窗口可见(即使它在屏幕外的某个地方),您就可以这样做: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, 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. This is what changed:    -----#--------------------------------------------#Make window visible so we can get some geometrywindow.update_idletasks()#Calculate title bar and border sizetitlebarHeight = window.winfo_rooty() - window.winfo_y()borderSize= window.winfo_rootx() - window.winfo_x()#Start with full available screenwindowWidth = screenAvailableWidthwindowHeight = screenAvailableHeight#Adjust for title bar and borderswindowWidth = windowWidth - (borderSize * 2 )windowHeight = (windowHeight - titlebarHeight) - borderSize#--------------------------------------------#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 screen (after adjustment)window.geometry("{}x{}+0+0".format(windowWidth,windowHeight))#Show the windowwindow.mainloop()(突破是在此处检查可疑重复问题(及其评论和回复)中提到的所有属性:tkinter window get x, y, geometry/coordinates without top of window 该问题并没有真正将各个部分放在一个新手中-友好的方式,也没有任何可用的示例代码,因此希望将来对其他人有用。)对于所有为我发表评论并提供解决方案的人,谢谢!我真的很感谢你花时间这样做。:-)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python