将.png文件编码到tkinter

时间:2017-04-13 08:38:15

标签: python image tkinter

我一直在尝试以.png格式对图像进行编码,这样每个人都可以在不更改代码的情况下查看我的应用中的图像,但是当我尝试使用代码时,我的代码并没有运行。我从未使用过base64,但我正在尝试对它进行编码,任何帮助都会非常感激!另外,我如何使这段代码更短/更整洁? 我收到此错误: SyntaxError :( unicode error)' unicodeescape'编解码器无法解码位置2-3中的字节:当我尝试运行我的代码时,截断的\ UXXXXXXXX转义抱歉缺少详细信息..此网站和python的新内容

#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()

0 个答案:

没有答案