[Python] 纯文本查看 复制代码
from tkinter import *
from tkinter import messagebox
from PIL import Image
from PIL import ImageTk
#修改了一下,可以打开其他类型的图片了,图片打开后缩放了,以便全部显示出来
def close_window():
messagebox.showinfo(title="警告", message="不许关闭,好好回答")
return
def love():
love2 = Toplevel(window)
love2.geometry("300x100+520+260")
love2.title("好巧,我也是")
label2 = Label(love2, text="好巧,我也是", font=("微软雅黑", 20))
label2.pack()
btn2 = Button(love2, text="确定", width=10, height=2, command=close_all_window)
btn2.pack()
love2.protocol("WM_DELETE_WINDOW", close_love)
def close_love():
return
def close_all_window():
window.destroy()
def no_love():
no_love2 = Toplevel(window)
no_love2.geometry("300x100+520+260")
no_love2.title("再考虑考虑")
label2 = Label(no_love2, text="再考虑考虑呗", font=("微软雅黑", 25))
label2.pack()
btn2 = Button(no_love2, text="好的", width=10, height=2, command=no_love2.destroy)
btn2.pack()
no_love2.protocol("WM_DELETE_WINDOW", close_no_love)
def close_no_love():
no_love()
def f1(w_, h_, png_image):
'''
w_ : 要适应的窗口宽
h_ : 要适应的窗口高
png_image : Image.open后的图片
'''
w, h = png_image.size # 获取图像的原始大小
f1 = 1.0 * w_ / w
f2 = 1.0 * h_ / h
factor = min([f1, f2])
width = int(w * factor)
height = int(h * factor)
return png_image.resize((width, height), Image.ANTIALIAS)
window = Tk()
window.title("你喜欢我吗?")
window.geometry('380x450')
window.geometry('+500+240')
window.protocol("WM_DELETE_WINDOW", close_window)
label = Label(window, text="hey,美羊羊", font=("微软雅黑", 15), fg='red')
label.grid(row=0, column=0, sticky=W)
label1 = Label(window, text="喜欢我吗?", font=("微软雅黑", 30))
label1.grid(row=1, column=1, sticky=E)
image = Image.open("d:0.jpg")
png_image_resized = f1(380, 280, image)
photo = ImageTk.PhotoImage(png_image_resized)
#photo = PhotoImage(file="d:0.jpg")
image_label = Label(window, image=photo)
image_label.grid(row=2, columnspan=2)
btn = Button(window, text="喜欢", width=15, height=2, command=love)
btn.grid(row=3, column=0, sticky=W)
btn1 = Button(window, text="不喜欢", command=no_love)
btn1.grid(row=3, column=1, sticky=E)
window.mainloop()