为什么 destroy() 在此代码中不起作用?
Why does destroy() not work in this code?
这是我正在尝试编写的一个简单测试脚本,它将帮助我自学 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()
当我运行这段代码时它returns出错了?
File "C:\Users6341\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
作为全局变量然后定义button2
的命令函数为
lambda: newScreen(window2, window1)
调用 newScreen
时 window2 和 window1 的值都是 None
,因此出现错误。这里的根本问题是您的 setWindow
函数:
def setWindow(windowEnter):
global window
window = windowEnter
window = Tk()
window.attributes("-fullscreen", True)
这与您的使用方式不符。当您调用 setWindow(window1)
时,您传递了 window1
的 值 ,在全局范围内看不到该函数对变量所做的操作。一个简单的例子是:
def increment(a):
a +=1
x = 1
print(x)
increment(x)
print(x)
这将打印 1 两次。
为了达到你想要的效果,我建议你使用字典来记录你的 windows。
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 newScreen(newScreen, screenToDelete)
,这非常 confusing/bad 风格,因为函数和它的第一个参数共享相同的姓名。无论如何我都改变了它以强调它现在将字符串作为参数,但请记住它以备将来使用。
这是我正在尝试编写的一个简单测试脚本,它将帮助我自学 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()
当我运行这段代码时它returns出错了?
File "C:\Users6341\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
作为全局变量然后定义button2
的命令函数为
lambda: newScreen(window2, window1)
调用 newScreen
时 window2 和 window1 的值都是 None
,因此出现错误。这里的根本问题是您的 setWindow
函数:
def setWindow(windowEnter):
global window
window = windowEnter
window = Tk()
window.attributes("-fullscreen", True)
这与您的使用方式不符。当您调用 setWindow(window1)
时,您传递了 window1
的 值 ,在全局范围内看不到该函数对变量所做的操作。一个简单的例子是:
def increment(a):
a +=1
x = 1
print(x)
increment(x)
print(x)
这将打印 1 两次。
为了达到你想要的效果,我建议你使用字典来记录你的 windows。
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 newScreen(newScreen, screenToDelete)
,这非常 confusing/bad 风格,因为函数和它的第一个参数共享相同的姓名。无论如何我都改变了它以强调它现在将字符串作为参数,但请记住它以备将来使用。