ttk.Treeview - Can't change row height

前端 未结 3 1053
余生分开走
余生分开走 2021-01-18 04:12

I\'m using ttkcalendar.py which can be found in this link.

I\'ve adapted it for use in Python 3.3

Basically what i\'m trying to do

3条回答
  •  醉梦人生
    2021-01-18 04:44

    I found that the Tkinter Font object has a metrics() method, that gives its height as "linespace". That allows the row height to be scaled dynamically:

    try:
        from tkinter.font import Font
        from tkinter.ttk import Style, Treeview
        from tkinter import *         
    except:
        from tkFont import Font
        font ttk import Style, Treeview
        from Tkinter import *
    
    font=Font(family='Arial', size=20)
    font.metrics()
    #output: {'ascent': 31, 'descent': 7, 'linespace': 38, 'fixed': 0}
    

    With that, you can get the font height with:

    font.metrics()['linespace']
    #output: 38
    

    Then use it to set the rowheight in your Treeview widget:

    fontheight=font.metrics()['linespace']
    
    style=Style()
    style.configure('Calendar.Treeview', font=font, rowheight=fontheight)   
    
    tree=Treeview(style='Calendar.Treeview')
    

    Changing the font object parameters comfortably updates the Treeview widget, but the rowheight doesn't get updated, and needs to be redone. So for example, scaling the font size with a keyboard shortcut may look like this:

    def scaleup():
        font['size']+=1
        style.configure('Calendar.Treeview', rowheight=font.metrics()['linespace'])
    
    def scaledown():
        font['size']-=1
        style.configure('Calendar.Treeview', rowheight=font.metrics()['linespace'])
    
    tree.bind('', scaleup)
    tree.bind('', scaledown)
    

    I actually wanted to do the same with Control-MouseWheel, but didn't figure out the behavior yet (Would be glad to hear how that works).

    Hope this comes handy.

提交回复
热议问题