简体   繁体   English

更新 tkinter GUI 以显示图像

[英]Updating tkinter GUI to show image

I am trying to create a GUI that shows your picture after you upload it.我正在尝试创建一个 GUI,在您上传图片后显示您的图片。 I wrote this code for it.我为此编写了这段代码。 But for some reason, it does not show the image.但由于某种原因,它不显示图像。 How can I update the root.mainloop() to show the image?如何更新 root.mainloop() 以显示图像?

displaynumber = 0

root = tk.Tk()

f1 = ''
v = tk.IntVar()
def ShowChoice():
    print(v.get())
tk.Label(root, text="""Choose method:""", justify = tk.LEFT, padx = 20).pack()

#uploading file
def selecting():
    global displaynumber
    root.filename =  filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
    f1 = root.filename
    displaynumber = 2
    print (root.filename)

# radio button
tk.Radiobutton(root, text="Select Image from Directory", padx = 20, variable=v, command=selecting, value=1).pack(anchor=tk.W)

# function for displaying image    
if displaynumber > 0:
    global img
    img = ImageTk.PhotoImage(Image.open(f1))
    panel = tk.Label(root, image=img)
    panel.pack(side="bottom", fill="both")
    root.update()   
elif displaynumber ==0:
    pass    

# root mainloop and geometry
root.geometry("1000x500")
root.mainloop()

The problem is that your if statement is not checked in mainloop .问题是您的if 语句未在mainloop中检查。 mainloop only updates the Tk instances that you added to root ; mainloop只更新你添加到root的 Tk 实例; thus, the code never re-enters your if statement.因此,代码永远不会重新进入您的 if 语句。 You could replace mainloop with your own loop that would contain the update_idletasks and update methods.您可以将mainloop替换为您自己的包含update_idletasksupdate方法的循环。 Here is a StackOverflow answer that did a good job explaining tk.mainloop .这是 StackOverflow 的答案,它很好地解释tk.mainloop

You also made an error when using Image.open() .您在使用Image.open()时也出错了。 This method takes a file and not a string, so you would need to open that file using the open method before you use it:此方法接受一个文件而不是一个字符串,因此您需要在使用它之前使用open方法打开该文件:

with open(f1) as img_file:
    img = ImageTk.PhotoImage(Image.open(f1))

Thus your code should look something like that:因此你的代码应该看起来像这样:

displaynumber = 0

root = tk.Tk()

f1 = ''
v = tk.IntVar()
def ShowChoice():
    print(v.get())

def selecting():
    global displaynumber
    global f1
    root.filename =  filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
    f1 = root.filename
    displaynumber = 2


tk.Label(root, text="""Choose method:""", justify = tk.LEFT, padx = 20).pack()

tk.Radiobutton(root, text="Select Image from Directory", padx = 20,  variable=v, command=selecting, value=1).pack(anchor=tk.W)

root.geometry("1000x500")

# Your new loop
while True:
    if displaynumber > 0:
        global fl
        with open(f1) as img_file:
            img = ImageTk.PhotoImage(Image.open(f1))
        panel = tk.Label(root, image=img)
        panel.pack(side="bottom", fill="both")
        displaynumber = 0
    elif displaynumber ==0:
        pass
    root.update_idletasks()
    root.update()

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

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