按钮,树形视图的自定义样式

时间:2019-02-04 08:52:58

标签: python tkinter ttk

我正在使用 Python tkinter 开发一个桌面应用程序。我在样式设计时遇到问题。有什么办法可以得到想要的输出。可能不准确,但有些相似,例如:button背景为红色/绿色/黄色,treeview背景为网格线。我尝试过

customed_style.configure('Custom.TButton', font=('Helvetica', 11), background="red", foreground='white')

customed_style.configure("Custom.Treeview.Heading", background="blue", foreground="white", relief="flat", font=('Helvetica', 10, 'bold'))

但是这些不能产生理想的输出,它们只会将文本颜色更改为白色,而不是背景。

我正在使用 Windows 10 操作系统

所需样式: Desired State

当前代码:

import tkinter as tk
from tkinter import ttk


class MainWindow:
    def __init__(self, master):
        self.master = master
        self.master.title("Python tkinter ttk Style")
        self.master.wm_iconbitmap('icon.ico')

        self.frame_buttons = ttk.Frame(self.master)
        self.frame_buttons.grid(row=0, column=0, padx=4, pady=4, sticky='nesw')

        self.frame_treeview = ttk.Frame(self.master)
        self.frame_treeview.grid(row=1, column=0, padx=4, pady=4, sticky='nesw')

        self.create_buttons()
        self.create_treeview()

    def create_buttons(self):
        self.details_button_image = tk.PhotoImage(file='details_button.png').subsample(3, 3)
        self.edit_button_image = tk.PhotoImage(file='edit_button.png').subsample(3, 3)
        self.delete_button_image = tk.PhotoImage(file='delete_button.png').subsample(3, 3)

        self.button_details = ttk.Button(self.frame_buttons, text="Details", image=self.details_button_image, compound="left", style='Custom.TButton')
        self.button_details.grid(row=0, column=0, padx=4, pady=4)

        self.button_edit = ttk.Button(self.frame_buttons, text="Edit", image=self.edit_button_image, compound="left", style='Custom.TButton')
        self.button_edit.grid(row=0, column=1, padx=4, pady=4)

        self.button_delete = ttk.Button(self.frame_buttons, text="Delete", image=self.delete_button_image, compound="left", style='Custom.TButton')
        self.button_delete.grid(row=0, column=2, padx=4, pady=4)

    def create_treeview(self):
        self.treeview_person_list = ttk.Treeview(self.frame_treeview, columns=('name', 'email', 'contact', 'address'), height=6)
        self.treeview_person_list.grid(row=0, column=0, padx=4, pady=4)

        self.treeview_person_list.heading('#0', text="ID")
        self.treeview_person_list.column("#0", minwidth=50, width=50, anchor='center')
        self.treeview_person_list.heading('#1', text="Full Name")
        self.treeview_person_list.column("#1", minwidth=150, width=150, anchor='w')
        self.treeview_person_list.heading('#2', text="Email")
        self.treeview_person_list.column("#2", minwidth=150, width=150, anchor='w')
        self.treeview_person_list.heading('#3', text="Phone")
        self.treeview_person_list.column("#3", minwidth=150, width=150, anchor='w')
        self.treeview_person_list.heading('#4', text="Street Address")
        self.treeview_person_list.column("#4", minwidth=150, width=150, anchor='w')

        self.treeview_person_list.insert('', 'end', text='01', values=('Annabel Geffen', 'ageffen0@hc360.com', '+55 138 953 4728', '34346 6th Pass'))
        self.treeview_person_list.insert('', 'end', text='02', values=('Ailey Myring', 'amyring1@lulu.com', '+234 874 785 2200', '72 Cody Street'))
        self.treeview_person_list.insert('', 'end', text='03', values=('Sherie Meeron', 'smeeron2@xing.com', '+86 348 534 4411', '0896 Express Park'))
        self.treeview_person_list.insert('', 'end', text='04', values=('Alice Grayston', 'agrayston3@phpbb.com', '+51 591 958 5938', '540 Beilfuss Circle'))
        self.treeview_person_list.insert('', 'end', text='05', values=('Hasheem Halbard', 'hhalbard4@wikia.com', '+593 493 130 1417', '08291 Shasta Parkway'))


def main():
    root = tk.Tk()

    customed_style = ttk.Style()
    customed_style.configure('Custom.TButton', font=('Helvetica', 11))

    app = MainWindow(root)
    root.mainloop()


if __name__ == '__main__':
    main()

当前样式: Current State

使用自定义样式代码:

import tkinter as tk
from tkinter import ttk


class MainWindow:
    def __init__(self, master):
        self.master = master
        self.master.title("Python tkinter ttk Style")
        self.master.wm_iconbitmap('icon.ico')

        self.frame_buttons = ttk.Frame(self.master)
        self.frame_buttons.grid(row=0, column=0, padx=4, pady=4, sticky='nesw')

        self.frame_treeview = ttk.Frame(self.master)
        self.frame_treeview.grid(row=1, column=0, padx=4, pady=4, sticky='nesw')

        self.create_buttons()
        self.create_treeview()

    def create_buttons(self):
        self.details_button_image = tk.PhotoImage(file='details_button.png').subsample(3, 3)
        self.edit_button_image = tk.PhotoImage(file='edit_button.png').subsample(3, 3)
        self.delete_button_image = tk.PhotoImage(file='delete_button.png').subsample(3, 3)

        self.button_details = ttk.Button(self.frame_buttons, text="Details", image=self.details_button_image, compound="left", style='Custom.TButton')
        self.button_details.grid(row=0, column=0, padx=4, pady=4)

        self.button_edit = ttk.Button(self.frame_buttons, text="Edit", image=self.edit_button_image, compound="left", style='Custom.TButton')
        self.button_edit.grid(row=0, column=1, padx=4, pady=4)

        self.button_delete = ttk.Button(self.frame_buttons, text="Delete", image=self.delete_button_image, compound="left", style='Custom.TButton')
        self.button_delete.grid(row=0, column=2, padx=4, pady=4)

    def create_treeview(self):
        self.treeview_person_list = ttk.Treeview(self.frame_treeview, columns=('name', 'email', 'contact', 'address'), height=6, style='Custom.Treeview')
        self.treeview_person_list.grid(row=0, column=0, padx=4, pady=4)

        self.treeview_person_list.heading('#0', text="ID")
        self.treeview_person_list.column("#0", minwidth=50, width=50, anchor='center')
        self.treeview_person_list.heading('#1', text="Full Name")
        self.treeview_person_list.column("#1", minwidth=150, width=150, anchor='w')
        self.treeview_person_list.heading('#2', text="Email")
        self.treeview_person_list.column("#2", minwidth=150, width=150, anchor='w')
        self.treeview_person_list.heading('#3', text="Phone")
        self.treeview_person_list.column("#3", minwidth=150, width=150, anchor='w')
        self.treeview_person_list.heading('#4', text="Street Address")
        self.treeview_person_list.column("#4", minwidth=150, width=150, anchor='w')

        self.treeview_person_list.insert('', 'end', text='01', values=('Annabel Geffen', 'ageffen0@hc360.com', '+55 138 953 4728', '34346 6th Pass'), tag='odd')
        self.treeview_person_list.insert('', 'end', text='02', values=('Ailey Myring', 'amyring1@lulu.com', '+234 874 785 2200', '72 Cody Street'), tag='even')
        self.treeview_person_list.insert('', 'end', text='03', values=('Sherie Meeron', 'smeeron2@xing.com', '+86 348 534 4411', '0896 Express Park'), tag='odd')
        self.treeview_person_list.insert('', 'end', text='04', values=('Alice Grayston', 'agrayston3@phpbb.com', '+51 591 958 5938', '540 Beilfuss Circle'), tag='even')
        self.treeview_person_list.insert('', 'end', text='05', values=('Hasheem Halbard', 'hhalbard4@wikia.com', '+593 493 130 1417', '08291 Shasta Parkway'), tag='odd')

        self.treeview_person_list.tag_configure('odd', background='#F5F5F5')
        self.treeview_person_list.tag_configure('even', background='#FFFFFF')


def main():
    root = tk.Tk()

    customed_style = ttk.Style()
    customed_style.configure('Custom.TButton', font=('Helvetica', 11), background="red", foreground='white')
    customed_style.configure('Custom.Treeview', highlightthickness=0, bd=0, font=('Helvetica', 10))
    customed_style.configure('Custom.Treeview.Heading', font=('Helvetica', 10, 'bold'), background="blue", foreground="red")

    app = MainWindow(root)
    root.mainloop()


if __name__ == '__main__':
    main()

具有自定义样式: Not Desired State

如果有人想尝试这些图像,以下是代码中使用的三个图像:

details_button: enter image description here 编辑按钮: enter image description here delete_button: enter image description here

对于 Python tkinter ttk 的完整自定义样式的任何好的资源,都将受到赞赏。预先感谢。

0 个答案:

没有答案