ttk.Treeview - Can't change row height

前端 未结 3 1054
余生分开走
余生分开走 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('<Control-equal>', scaleup)
    tree.bind('<Control-minus>', 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.

    0 讨论(0)
  • 2021-01-18 04:48

    Perhaps like you, I expected lines to expand as necessary. But I confirmed the problem with the code below, with the solution (the two style lines) omitted. When I could not find the solution here and the corresponding Style page, I googled and found this. Scroll down to Emiliano's answer, and some of the following (there is also an indent option).

    import tkinter as tk
    from tkinter import ttk
    
    root = tk.Tk()
    root.geometry('500x200')
    style = ttk.Style(root)
    style.configure('Treeview', rowheight=40)  #SOLUTION
    tree = ttk.Treeview(root)
    tree.insert('', 0, text='Line 1 of many XXX', tags='T')
    tree.insert('', 1, text='Line 2 of many XXX', tags='T')
    tree.insert('', 2, text='Line 3 of many XXX', tags='T')
    tree.column('#0', stretch=True)
    tree.tag_configure('T', font='Arial 20')
    tree.pack(fill='x')
    

    The above, with the answer omitted, is an example of minimal code that exhibits the problem. This is the sort of thing to post!

    EDIT 1:

    To make the Calendar widget properly importable and usable in another application, it should use a custom style, so its style does not affect any other treeviews in the app.

    style.configure('Calendar.Treeview', rowheight=40)
    tree = ttk.Treeview(root, style='Calendar.Treeview')
    

    EDIT 2:

    I am just learning about ttk styles myself. To answer your relief question, I went to this style doc and tried the following in Idle's Shell after running the above, with the two modifications in Edit 1.

    >>> style.layout('Calendar.Treeview')
    [('Treeview.field', {'sticky': 'nswe', 'children': [('Treeview.padding',
    {'sticky': 'nswe', 'children': [('Treeview.treearea', {'sticky': 'nswe'})]})], 'border': '1'})]
    >>> style.element_options('Calendar.Treeview.border')
    ('-relief',)
    >>> style.lookup('Calendar.Treeview.border', 'relief')
    ''
    >>> style.configure('Calendar.Treeview.border', relief='raised')
    {}
    

    I do not see any border nor did not see any effect of the setting. Perhaps relief applies to borders between columns. I don't know. (Note that changing rowheight is immediately available, so configuration is 'live'.)

    0 讨论(0)
  • 2021-01-18 04:49

    How can I change the height of the rows in the treeview as in prevoius attempts font size can be increased but the row height does not increase with font size. - STILL AWAITING HELP

    In case you are still awaiting help on this one, there is a way to change the row height, though that google groups thread says that it isn't officially supported by Tk:

    #apply any configuration options
    ttk.Style().configure('Treeview',rowheight=30)
    
    0 讨论(0)
提交回复
热议问题