2011-03-21 45 views
0

如何根据窗口标题长度计算并设置基于窗口标题长度的tkinter.Frame衍生窗口的宽度? 也就是说,有人可以帮我用width = ??? -expression在下面的代码? (心知肚明的结果取决于字体被使用,系统按钮等)根据标题长度计算tkinter窗口宽度

在此先感谢 /哈拉尔

import Tkinter as tkinter #Python 2 

class MyFrame(tkinter.Frame): 
    def __init__(self, title, *args, **kw): 
     tkinter.Frame.__init__(self, *args, **kw) 
      # Calculate the frame width ensuring that the entire title is shown 
      # using len(title) 
      width = ??? 
      self.master.config(width = width) 
      self.master.title(title) 

mf = MyFrame("Some Frame Title") 
mf.mainloop() 

回答

0

你不能这样做基于窗口的标题。显示器(以及宽度)由窗口管理器控制。你可以做到这一点的唯一方法是,如果你确定标题栏中正在使用什么字体。在这种情况下,您可以使用字体对象的measure1方法来计算该特定字体中字符串的宽度。即使是这样,你也需要知道窗口管理器可能会使用哪些额外的装饰并对此进行解释,而这对每个窗口管理器都是不同的。