【问题标题】:Python3: How to dynamically resize button text in tkinter/ttk?Python3:如何在 tkinter/ttk 中动态调整按钮文本的大小?
【发布时间】:2023-04-06 11:07:02
【问题描述】:

我想知道如何安排 ttk 小部件(例如标签或按钮)上的文本自动调整大小。

更改文本大小很容易,只需更改样式中的字体即可。然而,将它与窗口大小的变化挂钩有点棘手。在网上我发现了一些提示,但没有一个完整的答案发布。

所以,下面是一个完整的工作示例,作为我自己问题的答案。我希望有人觉得它有用。如果有人有进一步的改进建议,我会很高兴看到他们!

【问题讨论】:

    标签: button resize python-3.x tkinter ttk


    【解决方案1】:

    下面的例子展示了两种技术,一种是通过调整窗口大小来激活(参见resize()方法,绑定到<Configure>事件),另一种是直接改变字体大小(参见@ 987654323@ 方法)。

    其他需要调整大小的代码是__init__() 方法中的网格配置代码。

    在运行示例时,这两种方法之间存在一些交互,但我认为在“真实”情况下,一种技术就足够了,因此不会出现该问题。

    from tkinter import *
    from tkinter.ttk import *
    
    
    class ButtonApp(Frame):
        """Container for the buttons."""
    
        def __init__(self, master=None):
            """Initialize the frame and its children."""
    
            super().__init__(master)
            self.createWidgets()
    
            # configure the frame's resize behaviour
            master.columnconfigure(0, weight=1)
            master.rowconfigure(0, weight=1)
            self.grid(sticky=(N,S,E,W))
    
            # configure resize behaviour for the frame's children
            self.columnconfigure(0, weight=1)
            self.rowconfigure(0, weight=1)
            self.rowconfigure(0, weight=1)
    
            # bind to window resize events
            self.bind('<Configure>', self.resize)
    
    
        def createWidgets(self):
            """Make the widgets."""
    
            # this button mutates
            self.mutantButton = Button(self, text='Press Me',
                                       style='10.TButton')
            self.mutantButton.grid(column=0, row=0, sticky=(N,S,E,W))
            self.mutantButton['command'] = self.mutate
    
            # an ordinary quit button for comparison
            self.quitButton = Button(self, text='Quit', style='TButton')
            self.quitButton.grid(column=0, row=1, sticky=(N,S,E,W))
            self.quitButton['command'] = self.quit
    
    
        def mutate(self):
            """Rotate through the styles by hitting the button."""
    
            style = int(self.mutantButton['style'].split('.')[0])
            newStyle = style + 5
            if newStyle > 50: newStyle = 10
            print('Choosing font '+str(newStyle))
            self.mutantButton['style'] = fontStyle[newStyle]
    
            # resize the frame
    
            # get the current geometries
            currentGeometry = self._root().geometry()
            w, h, x, y = self.parseGeometry(currentGeometry)
            reqWidth = self.mutantButton.winfo_reqwidth()
            reqHeight = self.mutantButton.winfo_reqheight()
    
            # note assume height of quit button is constant at 20.
            w = max([w, reqWidth])
            h = 20 + reqHeight
            self._root().geometry('%dx%d+%d+%d' % (w, h, x, y))
    
    
        def parseGeometry(self, geometry):
            """Geometry parser.
            Returns the geometry as a (w, h, x, y) tuple."""
    
            # get w
            xsplit = geometry.split('x')
            w = int(xsplit[0])
            rest = xsplit[1]
    
            # get h, x, y
            plussplit = rest.split('+')
            h = int(plussplit[0])
            x = int(plussplit[1])
            y = int(plussplit[2])
    
            return w, h, x, y
    
    
        def resize(self, event):
            """Method bound to the <Configure> event for resizing."""
    
            # get geometry info from the root window.
            wm, hm = self._root().winfo_width(), self._root().winfo_height()
    
            # choose a font height to match
            # note subtract 30 for the button we are NOT scaling.
            # note we assume optimal font height is 1/2 widget height.
            fontHeight = (hm - 20) // 2
            print('Resizing to font '+str(fontHeight))
    
            # calculate the best font to use (use int rounding)
            bestStyle = fontStyle[10] # use min size as the fallback
            if fontHeight < 10: pass # the min size
            elif fontHeight >= 50: # the max size
                bestStyle = fontStyle[50]
            else: # everything in between
                bestFitFont = (fontHeight // 5) * 5
                bestStyle = fontStyle[bestFitFont]
    
            # set the style on the button
            self.mutantButton['style'] = bestStyle
    
    
    root = Tk()
    root.title('Alice in Pythonland')
    
    # make a dictionary of sized font styles in the range of interest.
    fontStyle = {}
    for font in range(10, 51, 5):
        styleName = str(font)+'.TButton'
        fontName = ' '.join(['helvetica', str(font), 'bold'])
        fontStyle[font] = styleName
        Style().configure(styleName, font=fontName)
    
    # run the app
    app = ButtonApp(master=root)
    app.mainloop()
    root.destroy()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-01
      相关资源
      最近更新 更多