Python 不知道是什么';是什么导致了这个错误;属性错误:'_io.TextIOWrapper';对象没有属性';tk'&引用;

Python 不知道是什么';是什么导致了这个错误;属性错误:'_io.TextIOWrapper';对象没有属性';tk'&引用;,python,python-3.x,tkinter,attributeerror,Python,Python 3.x,Tkinter,Attributeerror,代码不断抛出AttributeError:“\u io.TextIOWrapper”对象没有属性“tk”,我不知道是什么导致了它,我查看了其他帖子,但没有任何东西帮助我了解发生了什么 下面是导致它的代码 def showhwk(lesson, popup): lesson = lesson.replace("/","") popup.withdraw() show = Tk() show.title("Homework ma

代码不断抛出
AttributeError:“\u io.TextIOWrapper”对象没有属性“tk”
,我不知道是什么导致了它,我查看了其他帖子,但没有任何东西帮助我了解发生了什么

下面是导致它的代码

    def showhwk(lesson, popup):
        lesson = lesson.replace("/","")
        popup.withdraw()
        show = Tk()
        show.title("Homework marks for "+lesson)
        show.geometry("+{}+{}".format(positionRight, positionDown))
        try:
            with open(lesson+".csv", "r") as show:
                csvlist = list(csv.reader(show))
            for label in range (len(csvlist)):
                Label(show, text = "hello").grid(row = label)
        except FileNotFoundError:
            show.title("Error!")
            error = Label(show, text = "Homework file was not found")
            error.grid(row = 0)

        def goback3(show):
            popup.deiconify()
            show.withdraw()

        returnbut = Button(show, text = "Return", bg = "#79838e", command = lambda: goback3(show)).grid(row = 40, sticky = W+E)
这是全部错误:

Traceback (most recent call last):
  File "C:\Users\Olek\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)

  File "D:\Desktop\Folders\Python\Coursework\coursework code main.py", line 242, in <lambda>

show = Button(popup, text = "Show homework marks", bg = "green", command = lambda: showhwk(lesson, popup))

  File "D:\Desktop\Folders\Python\Coursework\coursework code main.py", line 278, in showhwk

Label(show, text = "hello").grid(row = label)

  File "C:\Users\Olek\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 3143, in __init__

Widget.__init__(self, master, 'label', cnf, kw)

  File "C:\Users\Olek\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2561, in __init__

BaseWidget._setup(self, master, cnf)

  File "C:\Users\Olek\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2530, in _setup

self.tk = master.tk

AttributeError: '_io.TextIOWrapper' object has no attribute 'tk'
回溯(最近一次呼叫最后一次):
文件“C:\Users\Olek\AppData\Local\Programs\Python\Python38-32\lib\tkinter\\ uuuuuuu init\uuuuuuuuu.py”,第1883行,在调用中__
返回self.func(*args)
文件“D:\Desktop\Folders\Python\Coursework\Coursework code main.py”,第242行,在
显示=按钮(弹出,text=“显示作业标记”,bg=“绿色”,命令=lambda:showhwk(课程,弹出))
showhwk中第278行的文件“D:\Desktop\Folders\Python\Coursework\Coursework code main.py”
标签(show,text=“hello”).grid(行=标签)
文件“C:\Users\Olek\AppData\Local\Programs\Python\Python38-32\lib\tkinter\\ uuuu init\uuuu.py”,第3143行,在__
小部件。_u初始化(自、主、标签、cnf、kw)
文件“C:\Users\Olek\AppData\Local\Programs\Python\Python38-32\lib\tkinter\\uuuuuu init\uuuuuu.py”,第2561行,在\uuu init中__
BaseWidget.\u设置(自、主、cnf)
文件“C:\Users\Olek\AppData\Local\Programs\Python\Python38-32\lib\tkinter\\uuuuuuu init\uuuuuuuuu.py”,第2530行,在安装程序中
self.tk=master.tk
AttributeError:“\u io.TextIOWrapper”对象没有属性“tk”

首先定义
show
如下:

show = Tk()
稍后,使用以下语句将
show
重新定义为打开的文件句柄:

with open(lesson+".csv", "r") as show:
然后,尝试使用
show
作为小部件的主控:

Label(show, text = "hello").grid(row = label)

因为
show
不再是一个小部件,所以它不能用作另一个小部件的主程序。这就是您出现tkinter错误的原因。

向我们显示完整错误,请:)现在已经这样做了,谢谢您正在使用上下文管理器重新分配
中的
show
,请正确设置错误格式。请显示从您发布的示例中得到的错误。错误与代码不匹配