简体   繁体   English

如何使用 Tkinter 在 Python 上上传图像?

[英]How do I upload an image on Python using Tkinter?

I am doing GUI programming using Tkinter on Python.我正在 Python 上使用 Tkinter 进行 GUI 编程。 I am using the grid manager to make widgets.我正在使用网格管理器制作小部件。 I have created several buttons and I want to upload an image on top of them.我创建了几个按钮,我想在它们上面上传一个图像。 When I enter this code, it gives me an escape sequence error .当我输入此代码时,它给了我一个escape sequence error

I heard using PIL is not a good idea?我听说使用 PIL 不是一个好主意? Is that true?真的吗?

cookImage = PhotoImage(file = "image/C:\Users\terimaa\AppData\Local\Programs\Python\Python36-32\cook.gif")

Windows filenames must be entered as raw strings: Windows 文件名必须作为原始字符串输入:

cookImage = PhotoImage(file=r"C:\Users\terimaa\AppData\Local\Programs\Python\Python36-32\cook.gif")

This applies to all of Python, not just PIL.这适用于所有 Python,而不仅仅是 PIL。

Use:用:

path = r"a string with the path of the photo" 

Note the r prefix, it means a raw string.注意r前缀,它表示一个原始字符串。

...
img = ImageTk.PhotoImage(Image.open(file=path))
label = tk.Label(root, image = img)
label.something() #pack/grid/place
...

The path can be:路径可以是:

  • Absolute ( "C:\\Users\\terimaa\\AppData\\Local\\Programs\\Python\\Python36-32\\cook.gif" )绝对 ( "C:\\Users\\terimaa\\AppData\\Local\\Programs\\Python\\Python36-32\\cook.gif" )

  • Relative ( "\\cook.gif" , depends on where the Python code is)相对( "\\cook.gif" ,取决于 Python 代码的位置)

If you have an image file that is exactly what you want, just open it with BitmapImage or PhotoImage .如果您有一个正是您想要的 图像文件,只需使用BitmapImagePhotoImage打开它。 Note that Tcl/Tk 8.6, which you should have with 3.6 on Windows, also reads .png files.请注意,Tcl/Tk 8.6(在 Windows 上使用 3.6 应该具有)也读取 .png 文件。 On Windows, prefix the filename with 'r' or use forward slashes: 'C:/User/...'.在 Windows 上,在文件名前加上 'r' 或使用正斜杠:'C:/User/...'。

The actual PIL package is no longer maintained and only works on 2.x.不再维护实际的 PIL 包,仅适用于 2.x。 That is what a new user should not use.那是新用户不应该使用的。 The compatible successor, pillow (installed for instance with python -m pip install pillow ) is actively maintained and works with 3.x.兼容的后继者pillow (例如使用python -m pip install pillow )得到积极维护并与 3.x 一起使用。 The compatibility extends to the import statement: import PIL imports pillow.兼容性扩展到导入语句: import PIL导入枕头。 Pillows allows one to manipulate images and to convert many formats to tk format (the ImageTk class). Pillows 允许操作图像并将多种格式转换为 tk 格式(ImageTk 类)。

this is exact code which is most help for move image这是对移动图像最有帮助的确切代码

from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import os, shutil

class Root(Tk):
    def __init__(self):
        super(Root,self).__init__()
        self.title("thinter Dialog Widget")
        self.minsize(640,400)

        self.labelFrame = ttk.LabelFrame(self,text="Open A File")
        self.labelFrame.grid(column=0,row=1,padx= 20, pady= 20)
        self.btton()

    def btton(self):
        self.button = ttk.Button(self.labelFrame, text="Browse Afile", command=self.fileDailog)
        self.button.grid(column=1,row=1)
    def fileDailog(self):
        self.fileName = filedialog.askopenfilename(initialdir = "/", title="Select A File",filetype=(("jpeg","*.jpg"),("png","*.png")))
        self.label = ttk.Label(self.labelFrame, text="")
        self.label.grid(column =1,row = 2)
        self.label.configure(text = self.fileName)
        os.chdir('e:\\')
        os.system('mkdir BACKUP')
        shutil.move(self.fileName,'e:\\')



if __name__ == '__main__':
    root = Root()
    root.mainloop()

you could not move image to c drive due to Permission denied: this code work successfully on python 3.8, 3,7由于权限被拒绝,您无法将图像移动到 c 驱动器:此代码在 python 3.8、3,7 上成功运行

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

相关问题 如何使用 python tkinter 将所选图像上传到 label? - How can I upload the selected image into the label with using python tkinter? 如何将图像放入 python tkinter? - How do I put an image in python tkinter? 如何使用 tkinter 调整此图像的大小? - How do I resize this image using tkinter? 如何使用上传按钮显示图像并使用 window 调整大小(动态)使用 Tkinter python 调整大小,我需要添加上传按钮 - How to display image with upload button in and resize it with window resize (Dynamically)with Tkinter python, I need to add upload button 在 Python Tkinter 中,如何使用名称获取嵌入在文本小部件中的图像的索引? - In Python Tkinter, how do I get the index of an image embedded in a text widget using the name? 如何在python中的tkinter中使用图像更新标签? - How do I update a label with an image in tkinter in python? 如何将 JPEG 图像插入 python Tkinter 窗口? - How do I insert a JPEG image into a python Tkinter window? 如何在 Python 中使用 Tkinter 中的 .create_image - How do I use the .create_image in Tkinter in Python 如何使用枕头将图像添加到我的 tkinter GUI? - How do I add an image using pillow to my tkinter GUI? 如何使用def语句在tkinter中更改图像 - How do i change an image in tkinter using a def statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM