后面的winfo_height/width中的值错误标签.配置(文本=)

2024-06-16 12:02:54 发布

您现在位置:Python中文网/ 问答频道 /正文

在python3.5.0中,在Windows上使用空闲Python上的Tkinter,我尝试在鼠标光标之后在各种缩略图上做一些悬停文本(比如web浏览器中图像上的alt文本),每个缩略图中都有不同长度的文本。你知道吗

因为鼠标可能会靠近窗口的边缘,导致文本离开可视空间的边缘,所以我想根据文本是否会离开来移动它。你知道吗

但我发现在调用标签上的configure(text=)之后,它有时会返回标签的旧高度/宽度值。我尝试过添加.after(),但似乎没有任何效果。你知道吗

def onEnter(self, event): # called by .bind("<Enter>", self.onEnter)
  print(time.time(), self.name, "onEnter")
  hoverlabel.config(text=self.name)
  self.onMove(event) # use the move event for code-reuse

def onMove(self, event): # called by .bind("<Motion>", self.onMove)
  # get the mouse's location on the current window
  win = hoverlabel.winfo_toplevel()
  winx = win.winfo_rootx()
  winy = win.winfo_rooty()
  xpos = event.x_root - winx
  ypos = event.y_root - winy

  # get the size of the text
  texth = hoverlabel.winfo_height()
  textw = hoverlabel.winfo_width()

  # This will sometimes print old values!
  print(time.time(), self.name, "onMove", textw, texth)

  # determine if the text would run off the edge
  EXTRA = 5 # a few extra pixels to eliminate some grossness
  if xpos + textw + EXTRA >= win.winfo_width():
      xpos = xpos - textw

  if ypos + (texth*2) >= win.winfo_height():
      ypos = ypos - texth
  else:
      ypos = ypos + texth

  hoverlabel.place(anchor=Tk.NW, x=xpos, y=ypos)

有时输出:

1509771933.4276707 Tsuro of the Seas onMove 96 21
1509771933.4414082 Tsuro of the Seas onMove 96 21
1509771933.4548104 Tsuro of the Seas onLeave
1509771933.4618150 Blood Rage: 5th Player Expansi onEnter
1509771933.4711444 Blood Rage: 5th Player Expansi onMove 96 21
1509771933.4831665 Blood Rage: 5th Player Expansi onMove 180 21
1509771933.4951801 Blood Rage: 5th Player Expansi onMove 180 21

我们看到在从Tsuro到Blood Rage的转变过程中,它会等到第二次onMove更新,这是我下一次移动鼠标的时候。你知道吗

我能做什么?我没有意识到延迟到Tk更新之后的诀窍是什么?我计划最终将图像嵌入标签中,以缓冲来自互联网的信息,所以对这种奇怪的文本延迟进行排序会很好。。。你知道吗

编辑:“文本的旧值”--->;“标签的旧高度/宽度值”


Tags: thetext文本selfevent标签winrage