受这个question的启发,我想为我的根窗口编写我自己的大小调整函数。 但是我注意到我的代码显示了一些性能问题。如果您快速调整它的大小,您可以看到窗口没有如我所愿找到其高度提示,它会“结巴”。(更像是一个摆动)
有人知道为什么会这样吗?我最好的猜测是tkinter
事件处理程序对它来说太慢了。
也许我做的数学不是最快的方法
也许我应该说我在不同的地方试过几次。我尝试过的另一种方法是使用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()
似乎性能问题与微软有关,而且是一个well known issue让大多数微软开发人员疯狂的问题
抖动/闪烁/跳跃
黑色边框
双图像/快速闪烁\跳转
我的mashine是:
Windows 10 Home; x64-base,Intel(R) Core(TM) i3-2120 @ 3.30GHz, 3300 MHz, 2Cores
与
Python 3.7.2 and tkinter 8.6
我能够通过在OnMotion方法的开头添加
update()
函数来解决这个问题我已经快速添加了一些小部件和一些事件,以便每个人都能注意到性能问题,如果注释行(在OnMotion方法的开头)被停用
self.update()
。我已经玩了代码,没有得到任何错误。希望这能解决你的问题相关问题 更多 >
编程相关推荐