Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将.png文件编码到tkinter_Python_Image_Tkinter - Fatal编程技术网

Python 将.png文件编码到tkinter

Python 将.png文件编码到tkinter,python,image,tkinter,Python,Image,Tkinter,我一直在尝试以.png格式对图像进行编码,这样每个人都可以在我的应用程序中查看图像,而无需更改代码,但我在尝试时代码不会运行,我从未使用base64,但正在尝试这样编码,任何帮助都将不胜感激!。还有,我该如何使代码更短/更整洁? 我得到这个错误: SyntaxError:(unicode错误)'UnicodeScape'编解码器无法解码位置2-3的字节:截断\UXXXXXXXX转义当我尝试运行代码时,很抱歉缺少详细信息..此站点和python仍然是新的 #导入所需的模型 将tkinter作为tk

我一直在尝试以.png格式对图像进行编码,这样每个人都可以在我的应用程序中查看图像,而无需更改代码,但我在尝试时代码不会运行,我从未使用base64,但正在尝试这样编码,任何帮助都将不胜感激!。还有,我该如何使代码更短/更整洁? 我得到这个错误: SyntaxError:(unicode错误)'UnicodeScape'编解码器无法解码位置2-3的字节:截断\UXXXXXXXX转义当我尝试运行代码时,很抱歉缺少详细信息..此站点和python仍然是新的

#导入所需的模型
将tkinter作为tk导入
从PIL导入图像,ImageTk#编辑:在我从安装枕头到计算机后,该模块对我有效”http://pillow.readthedocs.io/en/4.1.x/installation.html"
导入网络浏览器
导入base64
打开(“C:\Users\simon\Desktop\pictures\pizza\u pics\meat\u lovers.png”、“rb”)作为图像文件
image\u data\u base64\u encoded\u string=base64.b64encode(image\u file.read())
#添加程序tkinter的URL和窗口
URL=”http://pizzahut.com.au"
pizza=tk.tk()
pizza.title(“pizza应用程序”)
比萨饼几何(“400x500”)
pizza.configure(background=“red”)
im=照片图像(数据=图像\数据\基本64\编码\字符串)
#定义webbrowser函数
def open_ph(事件):
webbrowser.打开新选项卡(URL)
#标签和按钮列表
button=tk.button(pizza,text=“欢迎来到Awesome pizza”)
button.grid(列=0,行=0)
按钮。配置(前台=“白色”,后台=“黑色”)
checkbutton=tk.checkbutton(比萨饼,text=“肉食爱好者”)
网格(列=0,行=1,padx=(5,95))
选中按钮。配置(前台=“白色”,后台=“黑色”)
checkbutton2=tk.Checkbutton(pizza,text=“Supreme”)
checkbutton2.grid(列=0,行=3,padx=(10120))
选中按钮2.配置(前台=“白色”,后台=“黑色”)
checkbutton3=tk.Checkbutton(比萨饼,text=“素食者”)
checkbutton3.grid(列=0,行=5,padx=(10120))
选中按钮3.配置(前台=“白色”,后台=“黑色”)
choice=tk.OptionMenu(pizza,tk.IntVar(),“选择您的尺码!”,“小”、“中”、“大”)
网格(列=1,行=0,padx=(5,95))
选择。配置(前台=“白色”,后台=“黑色”)
name_label=tk.label(text=“name”)
name_label.grid(列=1,行=1,padx=(10120))
名称\标签。配置(前台=“白色”,后台=“黑色”)
name_entry=tk.entry()
name_entry.grid(列=1,行=2,padx=(5,95))
编号\u标签=tk.标签(text=“联系人编号”)
数字_label.grid(列=1,行=3,padx=(5,95))
数字标签。配置(前台=“白色”,后台=“黑色”)
编号\条目=tk.entry()
数字\u entry.grid(列=1,行=4,padx=(5,95))
地址\标签=传统标签(text=“交货地址”)
地址_label.grid(列=1,行=5,padx=(5,95))
地址标签配置(前台=“白色”,后台=“黑色”)
地址\条目=tk.entry()
address_entry.grid(列=1,行=6,padx=(5,95))
#定义留言板的打印功能
def消息_customer():
打印(name\u entry.get())
打印(编号\u entry.get())
打印(地址\条目.get())
name=Person(name\u entry.get()、number\u entry.get()、address\u entry.get())
打印(姓名、姓名、号码、姓名、地址)
text_answer=tk.text(主窗口=窗口,高度=5,宽度=15)
text_answer.grid(列=1,行=7,padx=(10120))
text_answer.insert(“感谢您{name}订购我们的比萨饼,它应该在30分钟内准备好,祝您愉快!”。格式(name=name.name,number=number.number,address=address.address))
#运行留言板的按钮
单击按钮=tk.按钮(text=“完成订单”,命令=message\u客户)
单击按钮。网格(列=1,行=7,padx=(5,95))
单击按钮。配置(前台=“白色”,后台=“黑色”)
班长:
定义初始化(self,name,number=None,address=None):
self.name=名称
self.number=number
self.address=地址
#将所有图像输入指定的点
im=Image.open(r“C:\Users\simon\Desktop\pictures\pizza\u pics\meat\u lovers.png”)
im.缩略图((100100),图像.反别名)
photo=ImageTk.PhotoImage(图像)
标签\图像=tk.标签(图像=照片)
label_image.grid(列=0,行=2,padx=(5,95))
第二张照片=Image.open(r“C:\Users\simon\Desktop\picts\random\u costude\pics\supreme.png”)
第二张照片。缩略图((100100),图像。反别名)
第二张照片=ImageTk.PhotoImage(第二张照片)
标签第二张照片=tk.标签(图像=第二张照片)
label_second_photo.grid(列=0,行=4,padx=(5,95))
第三张图片=Image.open(r“C:\Users\simon\Desktop\pictures\pizza\u pics\veggie.png”)
第三张图片。缩略图((100100),图片。反别名)
第三张图片=ImageTk.PhotoImage(第三张图片)
标签第三张图片=tk.标签(图片=第三张图片)
标签第三个图片网格(列=0,行=6,padx=(5,95))
#让窗口正常运行,并使用第一个按钮连接到internet
按钮绑定(“,打开)
pizza.mainloop()

“代码未运行”不是一个有用的错误描述。请你再精确一点好吗?代码中一个明显的错误是没有对
PhotoImage
进行任何定义。很抱歉,我收到了这个错误:SyntaxError:(unicode错误)'UnicodeScape'编解码器无法解码位置2-3的字节:截断\uxxxxx转义。看看你是如何使用
图像完成的。在你的代码中打开
。@常识谢谢,修复了:)
#Imports the moduels needed
import tkinter as tk 
from PIL import Image, ImageTk #EDIT: This module worked for me after I installed Pillow in my computer from "http://pillow.readthedocs.io/en/4.1.x/installation.html"
import webbrowser
import base64
with open("C:\Users\simon\Desktop\pictures\pizza_pics\meat_lovers.png", "rb") as image_file
    image_data_base64_encoded_string = base64.b64encode(image_file.read())

#adds the URL and window for the program tkinter
URL = "http://pizzahut.com.au"
pizza = tk.Tk()
pizza.title("Pizza App")
pizza.geometry("400x500")
pizza.configure(background="red")
im = PhotoImage(data=image_data_base64_encoded_string)

#defines the webbrowser function
def open_ph(event):
    webbrowser.open_new_tab(URL) 

#List of labels and buttons
button = tk.Button(pizza, text="Welcome to Awesome Pizza")
button.grid(column=0, row=0)
button.configure(foreground="white", background="black")

checkbutton = tk.Checkbutton(pizza, text="Meat Lovers")
checkbutton.grid(column=0, row=1, padx=(5, 95))
checkbutton.configure(foreground="white", background="black")

checkbutton2 = tk.Checkbutton(pizza, text="Supreme")
checkbutton2.grid(column=0, row=3, padx=(10, 120))                      
checkbutton2.configure(foreground="white", background="black")

checkbutton3 = tk.Checkbutton(pizza, text="Vegetarian")
checkbutton3.grid(column=0, row=5, padx=(10, 120))
checkbutton3.configure(foreground="white", background="black")

choice = tk.OptionMenu(pizza, tk.IntVar(), "Select your size!", "Small", "Medium", "Large")
choice.grid(column=1, row=0, padx=(5, 95))
choice.configure(foreground="white", background="black")

name_label = tk.Label(text="Name")
name_label.grid(column=1, row=1, padx=(10, 120))
name_label.configure(foreground="white", background="black")

name_entry = tk.Entry()
name_entry.grid(column=1, row=2, padx=(5, 95))

number_label = tk.Label(text="Contact number")
number_label.grid(column=1, row=3, padx=(5, 95))
number_label.configure(foreground="white", background="black")

number_entry = tk.Entry()
number_entry.grid(column=1, row=4, padx=(5, 95))

address_label = tk.Label(text="Delivery address")
address_label.grid(column=1, row=5, padx=(5, 95))
address_label.configure(foreground="white", background="black")

address_entry = tk.Entry()
address_entry.grid(column=1, row=6, padx=(5, 95))

#defines the print function for the message board
def message_customer():
    print(name_entry.get())
    print(number_entry.get())
    print(address_entry.get())
    name = Person(name_entry.get(), number_entry.get(), address_entry.get()) 
    print (name.name, name.number, name.address)                                                     
    text_answer = tk.Text(master=window, height=5, width=15)
    text_answer.grid(column=1, row=7, padx=(10, 120))
    text_answer.insert("Thank you {name} for ordering our pizza, it should be ready within 30 mintues!, have a great day!".format(name=name.name, number=number.number, address=address.address))

#the button that runs the message board
click_button = tk.Button(text="Complete Order", command=message_customer)
click_button.grid(column=1, row=7, padx=(5, 95))
click_button.configure(foreground="white", background="black")

class Person:
    def __init__(self, name, number=None, address=None):
        self.name = name
        self.number = number
        self.address = address

#Inputs all the images into it's designated spots
im = Image.open(r"C:\Users\simon\Desktop\pictures\pizza_pics\meat_lovers.png") 
im.thumbnail((100, 100), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image)
label_image = tk.Label(image=photo)
label_image.grid(column=0, row=2, padx=(5, 95))

second_photo = Image.open(r"C:\Users\simon\Desktop\pictures\random_costume_pics\supreme.png") 
second_photo.thumbnail((100, 100), Image.ANTIALIAS)
second_photo = ImageTk.PhotoImage(second_photo)
label_second_photo = tk.Label(image=second_photo)
label_second_photo.grid(column=0, row=4, padx=(5, 95))

third_picture = Image.open(r"C:\Users\simon\Desktop\pictures\pizza_pics\veggie.png") 
third_picture.thumbnail((100, 100), Image.ANTIALIAS)
third_picture = ImageTk.PhotoImage(third_picture)
label_third_picture = tk.Label(image=third_picture)
label_third_picture.grid(column=0, row=6, padx=(5, 95))

#lets the window run properly and the first button to connect to the internet
button.bind("<Button-1>", open_ph)
pizza.mainloop()