简体   繁体   English

Tkinter grid_forget 正在清除框架

[英]Tkinter grid_forget is clearing the frame

    from tkinter import *
from PIL import ImageTk,Image
root=Tk()
root.title("Image Viewer")

def buttonforward(image_number):
    global myLabel
    myLabel.grid_forget()

    myLabel = Label(image=imagelist[image_number-1])
    myLabel.grid(row=0, column=0, columnspan=3)
    return
my_img1 = ImageTk.PhotoImage(Image.open('mountain1.jpg'))
my_img2 = ImageTk.PhotoImage(Image.open('mountain2.jpg'))
my_img3 = ImageTk.PhotoImage(Image.open('mountain3.jpg'))
my_img4 = ImageTk.PhotoImage(Image.open('mountain4.jpg'))
my_img5 = ImageTk.PhotoImage(Image.open('mountain5.jpg'))

myLabel = Label(image=my_img1, ).grid(row=0, column=0, columnspan=3)
imagelist = [my_img1, my_img2, my_img3, my_img4, my_img5]
button_back = Button(root, text='<<').grid(row=1,column=0)
button_exit = Button(root, text='Exit', padx=60, command=root.quit).grid(padx=60, row=1,column=1)
button_forward = Button(root, text='>>',command = lambda: buttonforward(2) ).grid(row=1,column=2)
root.mainloop()

myLabel.grid_forget() is not working and I am encountering the following error after I press the forward '>>' button: myLabel.grid_forget() AttributeError: 'NoneType' object has no attribute 'grid_forget' myLabel.grid_forget() 不工作,按下前进“>>”按钮后遇到以下错误:myLabel.grid_forget() AttributeError: 'NoneType' object has no attribute 'grid_forget'

seperate the grid method and it will work.分离网格方法,它将起作用。 Every function in Python needs to return something and if nothing is returned 'None' will set by default. Python 中的每个 function 都需要返回一些内容,如果没有返回任何内容,则默认设置为“无”。 So your variable will become myLabel = None .所以你的变量会变成myLabel = None

Now that you know why that is bad behavior your should also do it for every other widget in your code.既然您知道为什么这是不好的行为,您也应该对代码中的每个其他小部件都这样做。

. .

Explaination解释

To show you what went wrong in your code look at this bit of code here:为了向您展示代码中出了什么问题,请查看此处的这段代码:

import tkinter as tk

root = tk.Tk()
x1 = tk.Label(text='x1')
x1.pack()
print(x1)
root.mainloop()

the Output should be: Output 应该是:

.!label

This tells me that x1 is assigned to the label.这告诉我 x1 已分配给 label。

Now take a look at this:现在看看这个:

import tkinter as tk

root = tk.Tk()

x1 = tk.Label(text='x1')
returned_by_layoutmanager = x1.pack()
print(x1)
print(returned_by_layoutmanager)
root.mainloop()

Your Output will be:您的 Output 将是:

.!label
None

If you may noticed, None was returned by the layoutmanger.如果您可能注意到,布局管理器返回None This is how python works, as soon as somthing is returned by a method/function the interpreter returns to the point he started reading the function/method.这就是 python 的工作方式,只要方法/函数返回某个东西,解释器就会返回到他开始读取函数/方法的点。 It's like the function tells I'm done keep going.这就像 function 告诉我已经完成了继续前进。

So if you do this:所以如果你这样做:

import tkinter as tk

root = tk.Tk()
x2 = tk.Label(text='x2').pack()
print(x2)
root.mainloop()

Your Output will be:您的 Output 将是:

None

To understand why None is assigned to x2 and not to .!label by this line here:要理解为什么None被分配给 x2 而不是.!label通过这里的这一行:

x2 = tk.Label(text='x2').pack()

Try this:尝试这个:

import tkinter as tk

root = tk.Tk()

x1 = tk.Label(text='x1')
x1 = x1.pack()
print(x1)

root.mainloop()

Your Output will be:您的 Output 将是:

None

Its just the same as you do it in your oneliner.它与您在 oneliner 中执行的操作相同。 First you assign x1 to the instance of Label class and then you assign x1 to None .首先将 x1 分配给Label class 的实例,然后将 x1 分配None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM