如何添加主题?

问题描述 投票:0回答:1

我最近刚拿起python并且我一直在研究一个名为“ToDoList.py”的项目。它已经完成但是我想添加一个按钮来改变GUI的主题使用tkinter / ttk但它不起作用。

这是错误:

Traceback (most recent call last):
  File "todolist.py", line 64, in <module>
    lbl_title = Label(root, text="ToDoList", bg="white")
  File "C:\Users\Sam\AppData\Local\Programs\Python\Python37-32\lib\tkinter\ttk.py", line 761, in __init__
    Widget.__init__(self, master, "ttk::label", kw)
  File "C:\Users\Sam\AppData\Local\Programs\Python\Python37-32\lib\tkinter\ttk.py", line 559, in __init__
    tkinter.Widget.__init__(self, master, widgetname, kw=kw)
  File "C:\Users\Sam\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2296, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: unknown option "-bg"

我不明白为什么这个错误是可能的,因为我还没有调整小部件

from tkinter import *

from tkinter import ttk

from tkinter.ttk import *

from ttkthemes import themed_tk as tk

import random

import tkinter.messagebox

#--------root style
root = Tk()
#--------root backgroud
root.configure(bg="white")
#--------root title
root.title("Reminder")
#--------root size
root.geometry("225x300")
#--------create empty list
tasks = []
#--------fuction
def darkmd():
    root.get_themes()
    root.set_theme("equilux")
#--------command
lbl_title = Label(root, text="ToDoList", bg="white")
lbl_title.grid(row=0, column=0)

lbl_display = Label(root, text="", fg="black", bg="white")
lbl_display.grid(row=0, column=1)

txt_input = Entry(root, width=20, fg="black", bg="white")
txt_input.grid(row=1, column=1)

bt_add_task = Button(root, text="Add Task", fg="black", bg="white",         command = add_task)
bt_add_task.grid(row=1, column=0)

bt_del_all = Button(root, text="Del all", fg="black", bg="white", command = del_all)
bt_del_all.grid(row=2, column=0)

bt_del_one= Button(root, text="Del", fg="black", bg="white", command = del_one)
bt_del_one.grid(row=3, column=0)

bt_sort_asc = Button(root, text="Sort (ASC)", fg="black", bg="white", command = sort_asc)
bt_sort_asc.grid(row=4, column=0)

bt_sort_desc = Button(root, text="Sort (DESC)", fg="black", bg="white", command = sort_desc)
bt_sort_desc.grid(row=5, column=0)

bt_total_task = Button(root, text="Num Of Task", fg="black", bg="white", command = total_task)
bt_total_task.grid(row=6, column=0)

bt_darkmd = Button(root, text="Darkmode", fg="black", bg="white", command = darkmd)
bt_darkmd.grid(row=7, column=0)

lb_tasks = Listbox(root,fg="black", bg="white")
lb_tasks.grid(row=2, column=1, rowspan=9)

#--------main
root.mainloop()
python tkinter themes ttk
1个回答
0
投票

评论:如何使用:ttkthemes

要使用ttkthemes更改为以下内容: 没有style.theme_use(...声明,因为这是在__init__(...做的alredy。

from ttkthemes import ThemedTk

class App(ThemedTk):
    def __init__(self):
        super().__init__("equilux")

        # ATTENTION!!
        # The following could fail as i couldn't test with `ThemedTk`
        # ATTENTION!!
        style = ttk.Style(self)
        style.configure("TLabel", background="white")

问题:如何添加主题?

首先你要明白,不要以不受控制的方式混合tkintertkinter.ttk小部件。只有tkinter.ttk小部件可以使用themestyle设计。


  1. 仅使用以下常见的import语句 import tkinter as tk import tkinter.ttk as ttk
  2. 要实例化ttk小部件,请使用: 注意:您不能在bg=小部件上使用ttklbl_title = ttk.Label(root, text="ToDoList")
  3. 应用范围广泛: 注意:在任何窗口小部件实例化之前和之前执行所有样式定义非常重要。 class App(tk.Tk): def __init__(self): super().__init__() style = ttk.Style(self) style.theme_use('clam') style.configure("TLabel", background="white") self.title("Tkinter Style") self.geometry("225x300") lbl_title = ttk.Label(self, text="ToDoList") lbl_title.grid(row=0, column=0) if __name__ == "__main__": App().mainloop()

用Python测试:3.5

© www.soinside.com 2019 - 2024. All rights reserved.