Tkinter grid_forget 正在清除框架 [英] Tkinter grid_forget is clearing the frame

查看:43
本文介绍了Tkinter grid_forget 正在清除框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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() 不工作,我在按>>"后遇到以下错误按钮:myLabel.grid_forget()AttributeError: 'NoneType' 对象没有属性 'grid_forget'

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'

推荐答案

分离网格方法,它会起作用.Python 中的每个函数都需要返回一些东西,如果没有返回任何东西'None' 将默认设置.所以你的变量将变成 myLabel = None.

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. So your variable will become myLabel = None.

既然您知道为什么这是不好的行为,您也应该对代码中的每个其他小部件执行此操作.

Now that you know why that is bad behavior your should also do it for every other widget in your code.

.

要向您展示代码中出了什么问题,请查看此处的这段代码:

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()

输出应该是:

.!label

这告诉我 x1 已分配给标签.

现在看看这个:

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()

您的输出将是:

.!label
None

如果您可能注意到,None 被布局管理器返回.这就是python的工作方式,只要方法/函数返回了一些东西,解释器返回到他开始读取函数/方法的点.就像函数告诉我已经完成继续前进一样.

If you may noticed, None was returned by the layoutmanger. 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. It's like the function tells I'm done keep going.

如果你这样做:

import tkinter as tk

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

您的输出将是:

None

通过此处的这一行了解为什么 None 被分配给 x2 而不是 .!label:

To understand why None is assigned to x2 and not to .!label by this line here:

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

试试这个:

import tkinter as tk

root = tk.Tk()

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

root.mainloop()

您的输出将是:

None

这和你在oneliner中做的一样.首先你分配x1Label类的实例,然后你分配x1 .

Its just the same as you do it in your oneliner. First you assign x1 to the instance of Label class and then you assign x1 to None.

这篇关于Tkinter grid_forget 正在清除框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆