Using Tkinter Themes in oop python

蓝咒 提交于 2020-12-15 06:08:35

问题


I'm trying to use ttkthemes but for some reason when I import it i just get errors, It's telling me AttributeError: module 'ttkthemes.themed_tk' has no attribute 'Tk' so I tried changing Tk to ThemedTk but it still didn't work, thanks for any help in advance. Here's the original code

from tkinter import *
import tkinter as tk  
from tkinter import ttk
from ttkthemes import themed_tk as tk
class SeaofBTCapp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.frames = {}
        for F in (StartPage,Task):
            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()
        frame.winfo_toplevel().geometry("1024x720")
        frame.configure(bg='#333130')


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

class Task(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

app = SeaofBTCapp()
app.mainloop()



回答1:


That's because you used from ttkthemes import themed_tk as tk.

When the next time you use tk.Tk(), it could be themed_tk.Tk().Apparently, Tk class is in the module tkinter, not themed_tk.(Although you has used import tkinter as tk,it has been covered by from ttkthemes import themed_tk as tk.)

You could use from ttkthemes import themed_tk as [another_name] then do what you want.




回答2:


You overwrote your tk wich represents the tkinter module with ttkthemes.themed_tk.

tk.Tk being the window of tkinter module you can't access it anymore



来源:https://stackoverflow.com/questions/62164128/using-tkinter-themes-in-oop-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!