Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何使用while循环修复此语法错误_Python 3.x - Fatal编程技术网

Python 3.x 如何使用while循环修复此语法错误

Python 3.x 如何使用while循环修复此语法错误,python-3.x,Python 3.x,我正试图使它这样,当你关闭修补程序GUI另一个打开,这个过程重复 我试过这个while命令,但它说的语法无效 从tkinter导入* root=Tk() photo=PhotoImage(file=“frombly.png”) 标签=标签(根,图像=照片) label.pack() root.mainloop() 而2>1 您缺少循环体。 现在它有了,但因为2>1总是真的,所以它是一个无限循环 from tkinter import * root = Tk() photo = PhotoIm

我正试图使它这样,当你关闭修补程序GUI另一个打开,这个过程重复

我试过这个while命令,但它说的语法无效

从tkinter导入*
root=Tk()
photo=PhotoImage(file=“frombly.png”)
标签=标签(根,图像=照片)
label.pack()
root.mainloop()
而2>1

您缺少循环体。 现在它有了,但因为2>1总是真的,所以它是一个无限循环

from tkinter import *

root = Tk()

photo = PhotoImage(file="scary.png")
label = Label(root, image=photo)
label.pack()

root.mainloop()

while 2 > 1:
  pass

打开tkinter窗口,您可以尝试无限次:

from tkinter import *
from PIL import Image, ImageTk

image = Image.open("scary.png")
photo = ImageTk.PhotoImage(image)

while True:
    root = Tk()
    label = Label(root, image=photo)
    label.pack()

    root.mainloop()
编辑 我已经添加了用于在Tkinter中打开任何格式图像的代码,因此您必须使用
pip install-pill
(PIL-package)安装所需的软件包


我在没有图像的情况下测试了它,它正在工作。希望有帮助

这不会使它再次打开图像,因为您的代码段中有无效语法,并且此答案解决了它。打开图像是您可以在另一个问题中提出的另一个问题。@PorkBurrito有关图像,请参考此问题-这只会导致程序在第一个窗口被破坏后冻结。@BryanOakley对于此while循环中的2>1条件,您还有什么期望?Plus在回答中明确表示,它只解决了OP所要求的语法错误。有关图像问题和无限次打开tkinter窗口@PorkBurrito的问题,请参阅下面我的解决方案