Label.configure(text =)

时间:2017-11-04 05:10:53

标签: python tkinter

在Windows 3.5上使用Tkinter在IDLE Python上使用Tkinter时,我试图在鼠标光标的各种缩略图上执行一些悬停文本(如在Web浏览器中的图像上的alt文本)各种长度的文字。

由于鼠标可能靠近窗口边缘并导致文本偏离可视空间的边缘,因此我希望根据文本是否熄灭来移动它。

但是我看到在我的标签上调用configure(text =)之后,它有时会返回Label的旧高度/宽度值。我尝试添加.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更新之前,我不知道延迟的诀窍是什么?我打算最终将图像嵌入到将从互联网缓冲的标签中,因此将这个奇怪的文本延迟排序为...会很棒...

修改:“旧文字值” - &gt; “标签的旧高度/宽度值”

0 个答案:

没有答案