如何添加主题? [英] How to add themes?

查看:27
本文介绍了如何添加主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近刚开始学习 python,我一直在做一个名为ToDoList.py"的项目.它已经完成,但我想添加一个按钮来使用 tkinter/ttk但它不起作用.

I just picked up python recently and I've been working on a project called "ToDoList.py".It's finished but I want to add a button to change the theme of the GUI using tkinter / ttk but its not working.

这是错误:

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"

我不明白为什么会出现这个错误,因为我还没有调整小部件

I don't understand why this error is possible since i haven't adjusted the widgets yet

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()

推荐答案

作为 ThemedTk 的替代方案,您可以使用 ThemedStyle.这样,您的代码将与使用标准 ttk 主题之一完全一样,只是您使用 style = ThemedStyle(root) 而不是 style = Style(root)<定义样式/代码>.然后你只需使用 style.theme_use() 来改变主题,你可以用 style.theme_names() 列出可用的主题.

As an alternative to ThemedTk, you can use ThemedStyle. This way your code will be exactly like if you were using one of the standard ttk themes except that you define your style with style = ThemedStyle(root) instead of style = Style(root). Then you simply use style.theme_use(<theme name>) to change theme and you can list the available themes with style.theme_names().

from tkinter import ttk
import tkinter as tk
from ttkthemes import ThemedStyle


#--------root style
root = tk.Tk()
#--------root backgroud
root.configure(bg="white")
#--------root title
root.title("Reminder")
#--------root size
root.geometry("225x300")

# white theme
style = ThemedStyle(root)
style.theme_use('arc')  # white style

#--------create empty list
tasks = []


#--------function
def darkmd():
    style.theme_use("equilux")  # only changes the theme of the ttk widgets
    # change style of tk widgets manually:
    bg = style.lookup('TLabel', 'background')
    fg = style.lookup('TLabel', 'foreground')
    root.configure(bg=style.lookup('TLabel', 'background'))
    lb_tasks.configure(bg=bg, fg=fg)


#--------command
lbl_title = ttk.Label(root, text="ToDoList")
lbl_title.grid(row=0, column=0)

lbl_display = ttk.Label(root, text="")
lbl_display.grid(row=0, column=1)

txt_input = ttk.Entry(root, width=20)
txt_input.grid(row=1, column=1)

bt_add_task = ttk.Button(root, text="Add Task")
bt_add_task.grid(row=1, column=0)

bt_del_all = ttk.Button(root, text="Del all")
bt_del_all.grid(row=2, column=0)

bt_del_one = ttk.Button(root, text="Del")
bt_del_one.grid(row=3, column=0)

bt_sort_asc = ttk.Button(root, text="Sort (ASC)")
bt_sort_asc.grid(row=4, column=0)

bt_sort_desc = ttk.Button(root, text="Sort (DESC)")
bt_sort_desc.grid(row=5, column=0)

bt_total_task = ttk.Button(root, text="Num Of Task")
bt_total_task.grid(row=6, column=0)

bt_darkmd = ttk.Button(root, text="Darkmode", command=darkmd)
bt_darkmd.grid(row=7, column=0)

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

#--------main
root.mainloop()

清晰主题:深色主题:

请注意,将主题设置为equilux"后,只有 ttk 小部件变暗.因此,您需要在 darkmd() 中手动更改 tk 小部件的颜色(就像我为 rootlb_tasks 所做的那样).

Note that only the ttk widgets become dark after setting the theme to "equilux". So you need to manually change the colors of your tk widgets in darkmd() (like I did for root and lb_tasks).

这篇关于如何添加主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆