AttributeError:类型对象'Robo4_Board没有属性'tk'

时间:2019-12-29 18:23:33

标签: tkinter

我的tkinter程序运行良好,但是加载时间很长。 我使用类,以便可以使用模块,而不是将整个程序放在一个文件/模块中(不确定哪个术语)。 我将CheckWinner脚本复制到新页面,并将其导入到主程序中。

现在我遇到有关属性的错误吗?

该代码位于Raspberry Pi上,很难提取到我的PC上。 Robo4_Board是定义屏幕和大多数变量的主要类。

错误 Tkinter回调中的异常 追溯(最近一次通话):   调用中的文件“ /usr/lib/python3.7/tkinter/init.py”,行1705     返回self.func(* args)   文件“ /home/pi/Documents/TESTS/ROBO4_TEST_B.py”,第16行,位于Combined_func f(* args,** kwargs)中   在CheckWinner_A robo4.br_y_0 = tk.Button(ROBO4_TEST_B.robo4,text =“ Red Wins”,padx = 2,pady = 2,bg =中,文件“ /home/pi/Documents/TESTS/ROBO4_TEST_CheckWinner_A.py”,第17行r,fg =“ black”)文件“ /usr/lib/python3.7/tkinter/init.py”,第2369行,在 init 小部件中。 init ((自我,主人,“按钮”,cnf,kw)文件“ /usr/lib/python3.7/tkinter/init.py”,第2292行,位于 init BaseWidget._setup(self,master,cnf)文件“ /usr/lib/python3.7/tkinter/init.py”,第2262行,在_setup self.tk = master .tk AttributeError:类型对象'Robo4_Board'没有属性'tk'

我该怎么做才能使链接正常工作? 不知道代码应该去哪里。 所以我粘贴了.txt文件

这是ROBO4_TEST_List_A

import tkinter as tk
from tkinter import *
g = "green"
r = "dark red"
y = "yellow"

class Robo4_List():

    robo4_B ={}
    robo4_B = [g,g]

这是ROBO4_TEST_CheckWinner_A

import tkinter as tk
from tkinter import *
from ROBO4_TEST_List_A import Robo4_List
import ROBO4_TEST_A as ROBO4_TEST_A
g = "green"
r = "dark red"
y = "yellow"

class CheckWinner():
    def CheckWinner_A():
        global redwins
        global yelwins
        print(Robo4_List.robo4_B[0])
        if (Robo4_List.robo4_B[0] == r):
            robo4.br_y_0=tk.Button(ROBO4_TEST_A.robo4,text = "Red Wins", padx=2, pady=2, bg=r, fg="black")
            robo4.br_y_0.place(x=200, y=670)
            redwins += 1
            robo4.after(robo4.windelay, robo4.show_frame, "PageOne")

这是ROBO4TEST_B

import tkinter as tk
from tkinter import *
from ROBO4_TEST_List_A import Robo4_List
from ROBO4_TEST_CheckWinner_A import *

redwins = 0
yelwins = 0

g = "green"
r = "dark red"
y = "yellow"

def combine_funcs(*funcs):
    def combined_func(*args, **kwargs):
        for f in funcs:
            f(*args, **kwargs)
    return combined_func

class Robo4_Board(tk.Tk):
    bpadx = 65
    bpady = 40
    bpadx1 = 0
    bpadx4 = 6.6
    sy = 2.8
    bpady1 = 14
    bpady2 = bpady1-sy
    global wtx
    global wtx2
    global wty
    wtx = 100
    wtx2 = 900
    wty = 670
    c_color = r
    bgcolor = g
    br_y_color = {}
    br_y_color = [g, g, g]
    windelay = 500
    scoreboard = 1500
    playagain = 2000
    redwin = 0
    yelwin = 0

    def __init__(self):
        tk.Tk.__init__(self)
        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, PageOne, ScoreBoard):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

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

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.parent = parent
        self.controller = controller
        sp_label = tk.Label(self, text="Let's Play ROBO 4")
        sp_label.pack(side="bottom", fill="x", pady=10)
br_y_0=tk.Button(self,activebackground="green",repeatdelay=10,padx=robo4.bpadx,             pady=robo4.bpady,bg=robo4.bgcolor,command=combine_funcs(b_all0,CheckWinner.CheckWinner_A))

        br_y_0.place(x=robo4.bpadx*robo4.bpadx1, y=robo4.bpady*robo4.bpady1)

        scoreboard=tk.Button(self,padx=2, pady=2, bg="white",\
                    text=("RED  =  {}   YELLOW  =  {}"\
                     .format(redwins, yelwins)),\
                    command=lambda: controller.show_frame("ScoreBoard"))

        robo4_check=tk.Button(self, padx=2, pady=2, bg="white",\
                    text=("PLAY AGAIN"),command=combine_funcs(lambda: controller.show_frame("StartPage")))

        robo4_check.place(x=300, y=670)
        scoreboard.place(x=600, y=670)

        controller.geometry("1000x700")

        whos_turn=tk.Button(self,text="Your Turn",padx=2,pady=2,bg=robo4.c_color)
        whos_turn.place(x=wtx,y=wty)
        whos_turn2=tk.Button(self,text="Your Turn",padx=2,pady=2,bg=robo4.c_color)
        whos_turn2.place(x=wtx2,y=wty)

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        po_label = tk.Label(self, text="WE HAVE A WINNER")
        po_label.pack(side="bottom", fill="x" , pady=10)
        back_play=tk.Button(self, wraplength= 140, padx=2, pady=2, bg="white",text=("Click to Play Again"),

command = combine_funcs(lambda:controller.show_frame(“ StartPage”)))             back_play.place(x = 300,y = 670)

class ScoreBoard(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        sb_label = tk.Label(self, text="Score Board")
        sb_label.pack(side="bottom", fill="x" , pady=10)
        checkscore=tk.Button(self, wraplength=100,padx=robo4.bpadx-44, pady=robo4.bpady-8, bg="white",\
                    text=("Check Score?"\
                     .format(redwins, yelwins)),
                    command=combine_funcs(lambda: controller.show_frame("StartPage")))
        checkscore.place(x=robo4.bpadx*robo4.bpadx4, y=robo4.bpady*robo4.bpady2)

def b_all0():
    if (Robo4_List.robo4_B[0] == "green"):
        Robo4_List.robo4_B[0] = robo4.c_color
robo4.br_y_0=tk.Button(robo4,activebackground=robo4.c_color,padx=robo4.bpadx, pady=robo4.bpady, bg=Robo4_List.robo4_B[0])
        robo4.br_y_0.place(x=robo4.bpadx*robo4.bpadx1, y=robo4.bpady*robo4.bpady1)
    if (robo4.c_color == "dark red"):
        robo4.c_color = "yellow"
    else:
        robo4.c_color = "dark red"
    print (Robo4_List.robo4_B[0:12])
    whos_turn=tk.Button(robo4,text="Your Turn",padx=2,pady=2,bg=robo4.c_color)
    whos_turn.place(x=wtx,y=wty)
    whos_turn2=tk.Button(robo4,text="Your Turn",padx=2,pady=2,bg=robo4.c_color)
    whos_turn2.place(x=wtx2,y=wty)

robo4 = Robo4_Board

if __name__ == '__main__':

    robo4 = Robo4_Board()
    robo4.mainloop()

1 个答案:

答案 0 :(得分:0)

TL; DR:您试图使用 class 而不是 widget 作为另一个小部件的父级。父母必须始终是小部件。


让我们看看错误:

Robo4_Board' has no attribute 'tk'

如果您在期望小部件时将除实际小部件以外的其他内容传递给其中的一个类,则Tkinter会引发此类错误。例如,如果您执行类似tk.Label("this is a string")的操作,则会得到错误AttributeError: 'str' object has no attribute 'tk'

对于您而言,错误消息是这样的:

AttributeError: type object 'Robo4_Board' has no attribute 'tk'

来自此行:

robo4.br_y_0=tk.Button(ROBO4_TEST_B.robo4,...)

在这种情况下,ROBO4_TEST_B.robo4是类的 class ,而不是类的 instance 。由于类不是小部件,因此tkinter会引发错误。

这可能是由于以下代码:

robo4 = Robo4_Board

您正在将robo4定义为,而不是该类的 instance

可能解决方案是执行robo4 = Robo4_Board()(注意结尾的()),但我不能肯定地说。我不知道您的代码中是否还有其他地方假设robo4是类而不是实例。您没有提供适当的[mcve],因此我无法运行您的代码。