Python 为什么destroy()在这段代码中不起作用?

Python 为什么destroy()在这段代码中不起作用?,python,python-3.x,tkinter,destroy,Python,Python 3.x,Tkinter,Destroy,这是一个简单的测试脚本,我试图写这将帮助我教自己关于tkinter from tkinter import * def hello(): print("U pressed it lol") global window1, window2 window2 = None window1 = None def setWindow(windowEnter): global window window = windowEnter window = Tk() window.

这是一个简单的测试脚本,我试图写这将帮助我教自己关于tkinter

from tkinter import *
def hello():
   print("U pressed it lol")

global window1, window2
window2 = None
window1 = None

def setWindow(windowEnter):
   global window
   window = windowEnter
   window = Tk()
   window.attributes("-fullscreen", True)

def newScreen(newScreen, screenToDelete):
   setWindow(newScreen)
   print("New Window Created")
   screenToDelete.destroy()
   print("He ded lol")

setWindow(window1)

def setStuff():
   button = Button(window1, text="hey", command=hello)
   label = Label(window1, text="YoYoYo My dude")
   button2 = Button(window1, text="Next Page", command = lambda: newScreen(window2, window1))

   button.pack()
   label.pack()
   button2.pack()

setStuff()
当我运行此代码时,它会返回一个错误

File "C:\Users\026341\Desktop\test.py", line 19, in newScreen
screenToDelete.destroy()
AttributeError: 'NoneType' object has no attribute 'destroy'
为什么这不起作用&我如何修复它

提前感谢:
顺便说一句,我正在使用python 3.6,我现在无法测试它,但我发现了一个错误源:

lambda: newScreen(window2, window1)
这将创建一个不接受任何参数的lambda函数,因此window2和window1将为None,并且None没有destroy方法,因此出现错误。相反,请尝试:

lambda window2, window1: newScreen(window2, window1)
你设定

window2 = None
window1 = None
作为全局变量,然后定义按钮2的命令函数

它使用值window2和window1调用newScreen,这两个值均为None,因此出现错误。这里的根本问题是setWindow函数:

def setWindow(windowEnter):
    global window
    window = windowEnter
    window = Tk()
    window.attributes("-fullscreen", True)
这和你使用它的方式不一样。调用SetWindow1时,传递window1的值,在全局范围内无法看到函数对变量所做的操作。一个简单的例子是:

def increment(a):
    a +=1
x = 1
print(x)
increment(x)
print(x)
它将打印1两次

为了实现你想要的,我建议你使用字典来跟踪你的窗口

from tkinter import *
def hello():
   print("U pressed it lol")

global window1, window2
windows = {}


def setWindow(window_name):
   windows[window_name] = Tk()
   windows[window_name].attributes("-fullscreen", True)

def newScreen(newScreen_name, screenToDelete_name):
   setWindow(newScreen_name)
   print("New Window Created")
   windows[screenToDelete_name].destroy()
   del windows[screenToDelete_name] #delete invalid entry from dict
   print("He ded lol")

setWindow("window1")

def setStuff():
   button = Button(windows["window1"], text="hey", command=hello)
   label = Label(windows["window1"], text="YoYoYo My dude")
   button2 = Button(windows["window1"], text="Next Page", command = lambda: newScreen("window2", "window1"))

   button.pack()
   label.pack()
   button2.pack()

setStuff()

另请注意:以前您的函数是def newScreennewScreen,screentodelet,这是一种非常混乱/糟糕的风格,因为函数及其第一个参数都使用相同的名称。无论如何,我还是对它进行了修改,以强调它现在将字符串作为参数,但请记住这一点,以备将来使用。

这是您的完整代码吗?你从未将窗口1和窗口2设置为除“无”之外的任何其他值我是否忽略了什么?哪一个可能是issue@FlyingTeller是的,这是完整的代码。。。在允许setWindow等功能正常工作的情况下,我该如何设置它们?请稍候,写一个更详细的答案该错误从字面上回答了标题中的问题。这返回错误:TypeError:缺少2个必需的位置参数:“window2”和“window1”啊,是的,当然,我匆匆忙忙地做完了这件事,本来应该测试一下的@FlyingTeller回答得很好。谢谢!现在我可以继续我的任务了@杰克,不客气。注意我刚才做的两次编辑。
from tkinter import *
def hello():
   print("U pressed it lol")

global window1, window2
windows = {}


def setWindow(window_name):
   windows[window_name] = Tk()
   windows[window_name].attributes("-fullscreen", True)

def newScreen(newScreen_name, screenToDelete_name):
   setWindow(newScreen_name)
   print("New Window Created")
   windows[screenToDelete_name].destroy()
   del windows[screenToDelete_name] #delete invalid entry from dict
   print("He ded lol")

setWindow("window1")

def setStuff():
   button = Button(windows["window1"], text="hey", command=hello)
   label = Label(windows["window1"], text="YoYoYo My dude")
   button2 = Button(windows["window1"], text="Next Page", command = lambda: newScreen("window2", "window1"))

   button.pack()
   label.pack()
   button2.pack()

setStuff()