• <tfoot id='rYyUY'></tfoot>
    <i id='rYyUY'><tr id='rYyUY'><dt id='rYyUY'><q id='rYyUY'><span id='rYyUY'><b id='rYyUY'><form id='rYyUY'><ins id='rYyUY'></ins><ul id='rYyUY'></ul><sub id='rYyUY'></sub></form><legend id='rYyUY'></legend><bdo id='rYyUY'><pre id='rYyUY'><center id='rYyUY'></center></pre></bdo></b><th id='rYyUY'></th></span></q></dt></tr></i><div id='rYyUY'><tfoot id='rYyUY'></tfoot><dl id='rYyUY'><fieldset id='rYyUY'></fieldset></dl></div>

      <bdo id='rYyUY'></bdo><ul id='rYyUY'></ul>
  • <small id='rYyUY'></small><noframes id='rYyUY'>

      <legend id='rYyUY'><style id='rYyUY'><dir id='rYyUY'><q id='rYyUY'></q></dir></style></legend>

        Base64 字符串到 tkinter 中的图像

        时间:2023-09-29
          <bdo id='Qq5Dc'></bdo><ul id='Qq5Dc'></ul>
          <legend id='Qq5Dc'><style id='Qq5Dc'><dir id='Qq5Dc'><q id='Qq5Dc'></q></dir></style></legend>

            <i id='Qq5Dc'><tr id='Qq5Dc'><dt id='Qq5Dc'><q id='Qq5Dc'><span id='Qq5Dc'><b id='Qq5Dc'><form id='Qq5Dc'><ins id='Qq5Dc'></ins><ul id='Qq5Dc'></ul><sub id='Qq5Dc'></sub></form><legend id='Qq5Dc'></legend><bdo id='Qq5Dc'><pre id='Qq5Dc'><center id='Qq5Dc'></center></pre></bdo></b><th id='Qq5Dc'></th></span></q></dt></tr></i><div id='Qq5Dc'><tfoot id='Qq5Dc'></tfoot><dl id='Qq5Dc'><fieldset id='Qq5Dc'></fieldset></dl></div>
              • <small id='Qq5Dc'></small><noframes id='Qq5Dc'>

                  <tbody id='Qq5Dc'></tbody>

                <tfoot id='Qq5Dc'></tfoot>

                • 本文介绍了Base64 字符串到 tkinter 中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在我的 GUI 中使用了一个图像,当我编译 .exe 或我的 .py 时,我不希望它作为外部资源.我想我应该将它编码为一个字符串,复制代码中的字符串并对其进行解码并将其提供给 Tkinter.试了一堆办法还是不行,代码如下:

                  I have a image i'm using in my GUI and i dont want it as an external resource when i compile the .exe or to my .py. I figured i should encode it to a string, copy the string in the code and decode it and serve it to Tkinter. Tried a bunch of solutions but still doesnt work, here is the code:

                  import tkinter as tk
                  from tkinter import filedialog
                  from tkinter import *
                  import PIL
                  from PIL import Image, ImageTk
                  import base64
                  
                  
                  stringimagine="something the print gave me"
                  
                  
                  imagine=open('logo.jpg','rb')
                  encoded=base64.b64encode(imagine.read())
                  print(encoded)
                  imagine2=base64.b64decode(stringimagine)
                  
                  
                  fereastra_principala = tk.Tk()
                  
                  
                  
                  poza=Label(fereastra_principala,image=imagine2)
                  poza.pack(fill='both',expand='yes')
                  
                  fereastra_principala.mainloop()
                  

                  对于此代码,我收到此错误:

                  to this code i receive this error:

                  File "C:Python34lib	kinter\__init__.py", line 2609, in __init__ Widget.__init__(self, master, 'label', cnf, kw)
                  File "C:Python34lib	kinter\__init__.py", line 2127, in __init__ (widgetName, self._w) + extra + self._options(cnf))_tkinter.TclError
                  

                  这就是我现在用来获取照片的方式,作为外部资源:

                  And this is how i use to get the photo now, as an external resource:

                  img=Image.open('logo.jpg')
                  image=ImageTk.PhotoImage(img)
                  poza=Label(fereastra_principala,image=image)
                  poza.pack()
                  

                  推荐答案

                  如果你有 base64 编码的数据,你需要使用 data 参数.但是,我认为这不适用于 jpg.不过,它适用于 .gif 图像.

                  If you have base64-encoded data, you need to use the data argument. However, I don't think this will work for jpg. It will work for .gif images though.

                  这就是 规范文档 所说的数据选项:

                  This is what the canonical documentation says for the data option:

                  将图像的内容指定为字符串.字符串应该包含二进制数据,或者对于某些格式,base64 编码的数据(目前保证支持 GIF 图像).字符串的格式必须是其中有一个将接受字符串数据的图像文件格式处理程序的格式之一.如果同时指定了 -data 和 -file 选项,则 -file 选项优先.

                  Specifies the contents of the image as a string. The string should contain binary data or, for some formats, base64-encoded data (this is currently guaranteed to be supported for GIF images). The format of the string must be one of those for which there is an image file format handler that will accept string data. If both the -data and -file options are specified, the -file option takes precedence.

                  示例

                  import Tkinter as tk
                  
                  IMAGE_DATA = '''
                      R0lGODlhEAAQALMAAAAAAP//AP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
                      AAAAAAAAAAAA
                  AAAAACH5BAEAAAIALAAAAAAQABAAQAQ3UMgpAKC4hm13uJnWgR
                      TgceZJllw4pd2Xpagq0WfeYrD7
                  2i5Yb+aJyVhFHAmnazE/z4tlSq0KIgA7
                  
                      '''
                  
                  root = tk.Tk()
                  image = tk.PhotoImage(data=IMAGE_DATA)
                  label = tk.Label(root, image=image, padx=20, pady=20)
                  label.pack()
                  
                  root.mainloop()
                  

                  这篇关于Base64 字符串到 tkinter 中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Plotly:如何向烛台图添加交易量 下一篇:将 ascii 字符串转换为 base64,不带“b";和引号

                  相关文章

                • <tfoot id='2AA1u'></tfoot>

                  <small id='2AA1u'></small><noframes id='2AA1u'>

                  <legend id='2AA1u'><style id='2AA1u'><dir id='2AA1u'><q id='2AA1u'></q></dir></style></legend>

                        <bdo id='2AA1u'></bdo><ul id='2AA1u'></ul>
                      <i id='2AA1u'><tr id='2AA1u'><dt id='2AA1u'><q id='2AA1u'><span id='2AA1u'><b id='2AA1u'><form id='2AA1u'><ins id='2AA1u'></ins><ul id='2AA1u'></ul><sub id='2AA1u'></sub></form><legend id='2AA1u'></legend><bdo id='2AA1u'><pre id='2AA1u'><center id='2AA1u'></center></pre></bdo></b><th id='2AA1u'></th></span></q></dt></tr></i><div id='2AA1u'><tfoot id='2AA1u'></tfoot><dl id='2AA1u'><fieldset id='2AA1u'></fieldset></dl></div>