【问题标题】:AttributeError: module 'tkinter.ttk' has no attribute 'Tk'AttributeError:模块“tkinter.ttk”没有属性“Tk”
【发布时间】:2020-05-23 16:36:36
【问题描述】:

我最近为我的 tkinter 项目安装了 ttkthemes 包,所以我认为我的代码中提到的所有 tk 都应该替换为 ttk 但由于某种原因我收到错误 AttributeError: module 'tkinter.ttk' has no attribute 'Tk。请帮助,感谢您提前提供任何帮助。 这是我的代码:

from tkinter import *
from tkinter import ttk
from ttkthemes import themed_tk as tk

class SeaofBTCapp(ttk.Tk):

    def __init__(self, *args, **kwargs):
        ttk.Tk.__init__(self, *args, **kwargs)
        container = ttk.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 (Task, YourAdressess):
            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):
        # for frame in self.frames.values():
        # frame.grid_remove()

        frame = self.frames[cont]
        frame.ttkraise()
        frame.winfo_toplevel().geometry("1024x720")
        frame.configure(bg='#333130')



class Task(ttk.Frame):
    def __init__(self, parent, controller):
        ttk.Frame.__init__(self, parent)
        c = Canvas(self, height=50, width=102400, bg="#333130")
        c.pack()
        taskbutton = ttk.Button(self, text='Task', command=lambda: controller.show_frame(Task))
        taskbutton_window = c.create_window(10, 12.5, anchor=ttk.NW, window=taskbutton)
        adressbutton = ttk.Button(self, text='Your Adressess', command=lambda: controller.show_frame(YourAdressess))
        adressbutton_window = c.create_window(104, 12.5, anchor=ttk.NW, window=adressbutton)

class YourAdressess(ttk.Frame):
    def __init__(self, parent, controller):
        ttk.Frame.__init__(self, parent)
        c = Canvas(self, height=50, width=1024, bg="#333130")
        c.pack()
        taskbutton = ttk.Button(self, text='Task', command=lambda: controller.show_frame(Task))
        taskbutton_window = c.create_window(10, 12.5, anchor=ttk.NW, window=taskbutton)
        adressbutton = ttk.Button(self, text='Your Adressess', command=lambda: controller.show_frame(YourAdressess))
        adressbutton_window = c.create_window(104, 12.5, anchor=ttk.NW, window=adressbutton)

app = SeaofBTCapp()
app.mainloop()

【问题讨论】:

    标签: python oop tkinter themes


    【解决方案1】:

    错误准确地告诉你哪里出了问题。 ttk 没有名为Tk 的类。您必须使用来自 tkinter 模块的那个。

    由于您对 tkinter 进行了通配符导入,因此可以使用 Tk

    class SeaofBTCapp(Tk):
    

    【讨论】:

    • 或者你必须用 pip 安装ttkthemes :)
    • @TheMaker:即使这样,ttk 也没有 Tk
    • 没错,但是如果你不安装ttkthemes,你会得到一个更大的错误,对吧?
    猜你喜欢
    • 2021-11-03
    • 2017-03-21
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    相关资源
    最近更新 更多