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 该问题并没有真正将各个部分放在一个新手中-友好的方式,也没有任何可用的示例代码,因此希望将来对其他人有用。)
对于所有为我发表评论并提供解决方案的人,谢谢!我真的很感谢你花时间这样做。:-)
添加回答
举报
