Tkinter如何更改TreeView字体

时间:2019-11-23 14:56:43

标签: tkinter

我正在尝试在tkinter树状视图中更改字体,但不知道如何去做。我可以更改treeview列标题字体,但不能更改行中的字体。下面的演示使用旋转框更改字体大小。我曾尝试使用“ TkTextFont”,但是它没有用,它改变了旋转框中的字体大小,但没有改变树形视图。

import tkinter as tk
import tkinter.ttk as ttk
import tkinter.font as tkfont


class MySpinbox(ttk.Spinbox):
    def __init__(self, master=None, delay=250, **kwargs):
        ttk.Spinbox.__init__(self, master, **kwargs)
        self.delay = delay  # repeatdelay in ms
        self.bind('<<Increment>>', self._on_increment)
        self.bind('<<Decrement>>', self._on_decrement)
        self._increment_lock = False
        self._decrement_lock = False

    def _unlock_increment(self):
        self._increment_lock = False

    def _on_increment(self, event):
        if self._increment_lock:
            return "break"  # stop the increment
        else:
            # generate a virtual event corresponding to when the spinbox
            # is actually incremented
            self.event_generate('<<ActualIncrement>>')
            self._increment_lock = True
            self.after(self.delay, self._unlock_increment)

    def _unlock_decrement(self):
        self._decrement_lock = False

    def _on_decrement(self, event):
        if self._decrement_lock:
            return "break"  # stop the increment
        else:
            # generate a virtual event corresponding to when the spinbox
            # is actually decremented
            self.event_generate('<<ActualDecrement>>')
            self._decrement_lock = True
            self.after(self.delay, self._unlock_decrement)


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.title('Treeview Demo')
        self.geometry('420x200+20+20')

        style = ttk.Style()

        var = tk.StringVar()
        var.set(9)

        tv = self.tv = ttk.Treeview(self)
        tv.grid(row=0, column=0, sticky='NSEW')

        def size():
            font = tkfont.nametofont('TkHeadingFont')
            font.config(size=var.get())
            style.configure('Treeview.Heading', font=font)

            font = tkfont.nametofont('TkTextFont')
            font.config(size=var.get())

        spn = MySpinbox(self, textvariable=var, values=list(range(0, 1000)), command=size)
        spn.grid(row=0, column=1, pady=5, padx=20)

        tv.heading('#0', text='Name')

        tv.insert('', '0', 'item1', text='Item 1')
        tv.insert('', '1', 'item2', text='Item 2')
        tv.insert('', '2', 'item3', text='Item 3')

        tv.insert('item1', '0', 'python1', text='Python 1')
        tv.insert('item1', '1', 'python2', text='Python 2')

        tv.insert('python1', '0', 'sub1', text='Sub item 1')
        tv.insert('python1', '1', 'sub2', text='Sub item 2')


def main():
    app = App()
    app.mainloop()


if __name__ == '__main__':
    main()

2 个答案:

答案 0 :(得分:0)

我只是想出了如何在树状视图中设置字体,如果有人遇到同样的问题,我将在此处发布解决方案。似乎treeview不响应命名字体,您必须使用以下命令配置标签 在树中插入项目时,请设置treeview.tag_configure()并设置标签参数。我认为这是唯一的方法。

import tkinter as tk
import tkinter.ttk as ttk
import tkinter.font as tkfont


class MySpinbox(ttk.Spinbox):
    def __init__(self, master=None, delay=100, **kwargs):
        ttk.Spinbox.__init__(self, master, **kwargs)
        self.delay = delay  # repeatdelay in ms
        self.bind('<<Increment>>', self._on_increment)
        self.bind('<<Decrement>>', self._on_decrement)
        self._increment_lock = False
        self._decrement_lock = False

    def _unlock_increment(self):
        self._increment_lock = False

    def _on_increment(self, event):
        if self._increment_lock:
            return "break"  # stop the increment
        else:
            # generate a virtual event corresponding to when the spinbox
            # is actually incremented
            self.event_generate('<<ActualIncrement>>')
            self._increment_lock = True
            self.after(self.delay, self._unlock_increment)

    def _unlock_decrement(self):
        self._decrement_lock = False

    def _on_decrement(self, event):
        if self._decrement_lock:
            return "break"  # stop the increment
        else:
            # generate a virtual event corresponding to when the spinbox
            # is actually decremented
            self.event_generate('<<ActualDecrement>>')
            self._decrement_lock = True
            self.after(self.delay, self._unlock_decrement)


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.title('Treeview Demo')
        self.geometry('420x200+20+20')

        style = ttk.Style()

        var = tk.StringVar()
        var.set(9)

        tv = self.tv = ttk.Treeview(self)
        tv.grid(row=0, column=0, sticky='NSEW')

        font = tkfont.nametofont('TkTextFont')
        tv.tag_configure('TkTextFont', font=tkfont.nametofont('TkTextFont'))
        style.configure('Treeview', rowheight=font.metrics('linespace'))

        def size():
            _font = tkfont.nametofont('TkHeadingFont')
            _font.config(size=var.get())
            style.configure('Treeview.Heading', font=_font)

            _font = tkfont.nametofont('TkTextFont')
            _font.configure(size=var.get())
            style.configure('Treeview', rowheight=_font.metrics('linespace'))

        spn = MySpinbox(self, textvariable=var, values=list(range(0, 1000)), command=size)
        spn.grid(row=0, column=1, pady=5, padx=20)

        tv.heading('#0', text='Name')

        tv.insert('', '0', 'item1', text='Item 1', tags='TkTextFont')
        tv.insert('', '1', 'item2', text='Item 2', tags='TkTextFont')
        tv.insert('', '2', 'item3', text='Item 3', tags='TkTextFont')

        tv.insert('item1', '0', 'python1', text='Python 1', tags='TkTextFont')
        tv.insert('item1', '1', 'python2', text='Python 2', tags='TkTextFont')

        tv.insert('python1', '0', 'sub1', text='Sub item 1', tags='TkTextFont')
        tv.insert('python1', '1', 'sub2', text='Sub item 2', tags='TkTextFont')


def main():
    app = App()
    app.mainloop()


if __name__ == '__main__':
    main()

答案 1 :(得分:0)

它可能会响应命名字体,但您必须猜出正确的名称。

tk.splitlist(tk.call("font", "names"))

会给你一个它使用的名字的列表。然后你可以尝试改变每一个的大小,看看它使用的是哪一个。