受这个问题的启发,我想为我的根窗口编写我自己的调整大小函数。但我只是注意到我的代码显示了一些性能问题。如果你快速调整它的大小,你会看到窗口没有像我希望的那样找到它的高度提示,它“断断续续”。(这更像是一个摆动)
有人知道为什么会这样吗?我最好的猜测是tkinter
事件处理程序对它来说太慢了。也许我做的数学不是最快的方法。
也许我应该说我确实update_idletasks()
在不同的地方尝试过几次。我尝试过的另一种方法是使用该after
方法,但它使情况变得更糟。
无论如何,这是一个示例代码:
import tkinter as tk
class FloatingWindow(tk.Tk):
def __init__(self):
super().__init__()
self.overrideredirect(True)
self.center()
self.label = tk.Label(self, text="Grab the upper-right corner to resize")
self.label.pack(side="top", fill="both", expand=True)
self.grip2 = tk.Label(self,bg='blue')
self.grip2.place(relx=1.0, rely=0, anchor="ne")
self.grip2.bind("<B1-Motion>",self.OnMotion)
def OnMotion(self, event):
abs_x = self.winfo_pointerx() - self.winfo_rootx()
abs_y = self.winfo_pointery() - self.winfo_rooty()
if abs_x >0:
x = self.winfo_rootx()
y = self.winfo_rooty()+abs_y
height = self.winfo_height()-abs_y
if height >0:
self.geometry("%dx%d+%d+%d" % (abs_x,height,
x,y))
def center(self):
width = 300
height = 300
screen_width = self.winfo_screenwidth()
screen_height = self.winfo_screenheight()
x_coordinate = (screen_width/2) - (width/2)
y_coordinate = (screen_height/2) - (height/2)
self.geometry("%dx%d+%d+%d" % (width, height,
x_coordinate, y_coordinate))
app=FloatingWindow()
app.mainloop()
Run Code Online (Sandbox Code Playgroud)
我发现的最优雅的方法是在动作事件中添加以下代码:
time.sleep(0.175)
self.update_idletasks()
Run Code Online (Sandbox Code Playgroud)
这样我有更少但更长的斯托特间隔。此外,您可以看到这里发生了什么。
我的机器是:
Windows 10 家庭版;x64-base,Intel(R) Core(TM) i3-2120 @ 3.30GHz, 3300 MHz, 2Cores
和
Python 3.7.2 和 tkinter 8.6
我只是注意到 Windows 本身还没有弄清楚这一点。如果您采用普通目录/文件夹并调整其大小,您将在客户区中看到与上面的示例相同的闪烁。唯一的区别似乎是它们没有删除背景的问题。因此,对于 Windows 10 和 11,案件目前似乎已经结束。
归档时间: |
|
查看次数: |
359 次 |
最近记录: |