【问题标题】:My Tkinter photo is packed, but it shows as a blank我的 Tkinter 照片已打包,但显示为空白
【发布时间】:2019-03-31 03:27:21
【问题描述】:

当我的主要框架(根)顶部有一个顶层框架时,我似乎无法让图片显示在屏幕上。这就是所谓的“框架”。我在这篇文章中包含的照片上圈出了 tkinter 框架的轮廓。当我调整图片大小时,绿框轮廓会发生变化,但图片本身不会出现。

我还尝试将它打包到我的主根窗口中,成功了,这表明这是一个顶级窗口问题。我只是不知道它是什么。有什么想法吗?

这是我的代码:

    def show_topLevelWindow():

      from tkinter import ttk
      print("Entered to show results")
      window_linkedin = Toplevel(root)
      window_linkedin.geometry('1000x590')

      frame = Frame(window_linkedin)
      frame.pack()

      error_frame = tkinter.Frame(frame, highlightbackground="green", highlightcolor="green", highlightthickness=1)

      error_label = Label(frame, text="It appears there are no results for the selected country")
      error_label.config(font=("Helvetica Neue", 20))        

      im_error = Image.open("./ressources/images_gui/aw_snap.png")
      im_error = im_error.resize((500, 500), Image.ANTIALIAS)
      im_error = ImageTk.PhotoImage(file = "./ressources/images_gui/aw_snap.png")
      im_error_label = Label(frame, image=im_error)

      try:
          if ....:
          ....unimportant code ....
          else:
              error_label.pack(in_=error_frame)            
              im_error_label.pack(in_=error_frame)  
              error_frame.pack(anchor="center")          
      except Exception as e:
          error_label.pack(in_=error_frame)            
          im_error_label.pack(in_=error_frame)            
          error_frame.pack(anchor="center")

Packed image shows as blank

【问题讨论】:

    标签: python image tkinter png toplevel


    【解决方案1】:

    您遇到的一个最重要的问题是您的图像没有被保存以供参考。如果您将global im_error 添加到函数的最顶部,您的图像将可见。

    也就是说您的代码存在一些问题,您应该更正。

    第一:不要在函数中导入。而是将所有导入都写在代码的顶部。

    第二:我不知道你为什么要这么做.pack(in_=error_frame)。这不是一个人真正需要的东西。只需确保您的标签已分配给正确的框架。 in_ 参数很少使用,可能大多数人从不使用它。我在这里已经两年了,这是我第一次看到有人使用这种论点。

    第三:您没有展示 Tkinter 的导入,但是根据您编写代码的方式,看起来您已经完成了:

    import tkinter
    from tkinter import *
    

    这是矫枉过正,不是一个好主意。只需执行import tkinter as tk 并确保在适用的地方使用tk. 前缀。

    这是你重新编写的代码:

    import tkinter.ttk as ttk
    import tkinter as tk
    from PIL import ImageTk, Image
    
    
    
    def show_toplevel_window():
        global im_error
        window_linkedin = tk.Toplevel(root)
        window_linkedin.geometry('1000x590')
    
        frame = tk.Frame(window_linkedin)
        frame.pack()
    
        error_frame = tk.Frame(frame, highlightbackground="green", highlightcolor="green", highlightthickness=1)
        error_frame.pack()
    
        error_label = tk.Label(frame, font=("Helvetica Neue", 20), text="It appears there are no results for the selected country")
        error_label.pack()  
    
        im_error = Image.open("./ressources/images_gui/aw_snap.png")
        im_error = im_error.resize((500, 500), Image.ANTIALIAS)
        im_error = ImageTk.PhotoImage(file = "./ressources/images_gui/aw_snap.png")
        im_error_label = tk.Label(error_frame, image=im_error)
        im_error_label.pack()
    
    root = tk.Tk()
    show_toplevel_window()
    root.mainloop()
    

    【讨论】:

    • 谢谢!!对于解决方案和您的提示:-)
    • @JUANRUIZDEBUSTILLO 很高兴为您提供帮助。
    猜你喜欢
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    相关资源
    最近更新 更多