毫无用处的表白神器

python的简单GUI,基于tkinter库建立,只需100行不到的代码,加上一张图片

关于打包exe的话 使用pyinstaller 在cmd中进行

配音时taller -F -w XXXX.py

from tkinter import *
from PIL import ImageTk, Image
from tkinter import messagebox
import random

def closeWindow():
    messagebox.showinfo(title='警告',message='不许关闭,请认真回答!')
    return

def nolove():
    no_love=Toplevel(window)
    no_love.title('不要急!')
    no_love.geometry('300x100+580+290')
    lable=Label(no_love,text='再考虑考虑呗',font=('微软雅黑',20))
    lable.pack()
    btn=Button(no_love,text='好吧',width=10,height=2,command=no_love.destroy)
    btn.pack()
    no_love.protocol("WM_DELETE_WINDOW",close_nolove)

def close_nolove():
    no_love=Toplevel(window)
    no_love.title('不要急!')
    i=random.randint(10,90)
    no_love.geometry('300x100+5'+str(i)+'+290')
    lable=Label(no_love,text='再考虑考虑呗',font=('微软雅黑',20))
    lable.pack()
    btn=Button(no_love,text='好吧',width=10,height=2,command=no_love.destroy,bg='red')
    btn.pack()
    no_love.protocol("WM_DELETE_WINDOW",close_nolove)

def love():
    love=Toplevel(window)
    love.title('嘿嘿')
    love.geometry('300x100+580+290')
    lable = Label(love, text='好巧我也是!', font=('微软雅黑', 20))
    lable.pack()
    btn3=Button(love,text='同意',width=10,height=2,command=closelove)
    btn3.pack()
    love.protocol("WM_DELETE_WINDOW", lovecloseno)

def closelove():
    window.destroy()

def lovecloseno():
    pass

# 创建窗口
window = Tk()
# 设置窗口标题
window.title("来自孤单弱小又无助的学弟")
# 设置窗口大小
window.geometry("385x385")
# 窗口位置
window.geometry("+456+169")
# 窗口无法关闭
window.protocol("WM_DELETE_WINDOW", closeWindow)

btn1 = Button(window, text="喜欢", width=15, height=2,command=love)
btn1.grid(row=3, column=0, sticky=W)
btn2 = Button(window, text="但是,我拒绝",command=nolove)
btn2.grid(row=3, column=1, sticky=E)

# 标签控件
label1 = Label(window,text = "hello,小姐姐" ,font = ('微软雅黑', 15),fg='red')
# 定位    grid(网格式布局)
label1.grid(row = 0,column = 0,sticky=W)
label2 = Label(window,text = "喜欢我吗?!" ,font = ('微软雅黑', 35))
# sticky 对齐方法   NSWE(方位)
label2.grid(row = 1,column = 1,sticky=E)

img = Image.open('./love.jpg')
photo = ImageTk.PhotoImage(img)
imageLable = Label(window, image=photo)
# columspan 组件所跨越的列数
imageLable.grid(row=2, columnspan=2)

# 显示窗口    消息循环
window.mainloop()

效果图

毫无用处的表白神器_第1张图片

毫无用处的表白神器_第2张图片

你可能感兴趣的:(python)