ttk.Treeview - Can't change row height

前端 未结 3 1058
余生分开走
余生分开走 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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'.)

提交回复
热议问题