简体   繁体   English

如何正确地将 tkinter 指向不同文件夹中的图像?

[英]How to correctly point tkinter towards an image in a different folder?

I am creating a tkinter application in which I am using custom button images, along with a custom button class in order for the button to have a hovering effect.我正在创建一个 tkinter 应用程序,在其中我使用自定义按钮图像以及自定义按钮类以使按钮具有悬停效果。 The script was working fine when the images and scripts were in the same folder, however, as soon as I tried using the "os" or "path" module, python gives me this error.当图像和脚本位于同一文件夹中时,脚本运行良好,但是,一旦我尝试使用“os”或“path”模块,python 就会出现此错误。

"_tkinter.TclError: image "N:\\Year 13\\Computer Science\\Project\\AMFC\\Project files/Buttons/App button.gif" doesn't exist" "_tkinter.TclError: image "N:\\Year 13\\Computer Science\\Project\\AMFC\\Project files/Buttons/App button.gif" 不存在"

I made sure that the image is actually existent in the path, and I tried building the path based on the "os.getcwd()" and "os.path.join" as well as the "path" module equivalent.我确保图像实际上存在于路径中,并且我尝试基于“os.getcwd()”和“os.path.join”以及等效的“path”模块构建路径。 I also tried building the absolute path to the image, however, it still didn't work.我也尝试建立图像的绝对路径,但是,它仍然不起作用。

work_folder = os.path.dirname(os.path.abspath(__file__))
button_path = os.path.join(work_folder, "Project files/Buttons")

Here is my custom button class:这是我的自定义按钮类:

class Button(tk.Button):
    def __init__(self, parent, default_background="", hover_background="", **kwargs):
        tk.Button.__init__(self, master=parent, **kwargs)
        self.default_background = tk.PhotoImage(file=default_background)
        self.hover_background = tk.PhotoImage(file=hover_background)
        self.configure(image=self.default_background)
        self.bind("<Enter>", self.on_enter)
        self.bind("<Leave>", self.on_leave)

    def on_enter(self, event):
        self.configure(image=self.hover_background)

    def on_leave(self, event):
        self.configure(image=self.default_background)

Here is how I apply it:这是我如何应用它:

import Customised_Widgets as cw    

cw.Button(self, text="Pure Core", default_background=os.path.join(button_path, "App button.gif"),
              hover_background=os.path.join(button_path, "Project files/Buttons/App button(hover).gif"),
              relief=tk.FLAT,highlightcolor="#E7E6E6",highlightbackground="#E7E6E6",
              bg="#E7E6E6", bd=0, padx=0, pady=0, borderwidth=0,
              highlightthickness=0, compound="center").place(x=434, y=270) 

I created the button in a separate module in order to keep my work more organised.我在一个单独的模块中创建了按钮,以使我的工作更有条理。 This separate module is in the same folder as the main script.这个单独的模块与主脚本位于同一文件夹中。

I don't know what to do, I tried giving the full path name to the image, it still didn't work.我不知道该怎么办,我尝试为图像提供完整路径名,但仍然无效。 It refuses to detect the image in the folder that I point tkinter to, even though it does exist...它拒绝检测我指向 tkinter 的文件夹中的图像,即使它确实存在......

Any help would be appreciated.任何帮助,将不胜感激。

You shouldn't use the Separator characters in the os.path.join .您不应在os.path.join使用 Separator 字符。 This method will use the correct separator for your path.此方法将为您的路径使用正确的分隔符。 It is different on different Operation systems (For Windows , it's '\\' and for Unix it's '/').它在不同的操作系统上是不同的(对于Windows ,它是 '\\',对于Unix它是 '/')。 Furthermore you can write a cross-platform software with this solution.此外,您可以使用此解决方案编写跨平台软件。

You should try the following implementation:您应该尝试以下实现:

os.path.join(button_path, "Project files", "Buttons", "App button(hover).gif")

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

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