代码如下:
from tkinter import *
from tkinter import messagebox
# 创建一个窗口
window = Tk()
# 设置窗口的大小
window.geometry('400x460+500+200')
window.title('你喜欢我吗?')
def closeWindow():
messagebox.showinfo(title='警告',message='不许关闭好好回答')
# 点击关闭的时候触发
window.protocol('WM_DELETE_WINDOW',closeWindow)
# 设置标签
lable1 = Label(window,text = 'hey,小姐姐',font=('微软雅黑',14))
# 网格式的显示
lable1.grid()
lable2 = Label(window,text = '喜欢我吗?',font=('微软雅黑',30))
# 网格式的显示 ,对齐方式 上下左右 N S W E
lable2.grid(row=1,column=1,sticky=W)
# 设置图片
photo = PhotoImage(file='./love.png') #在当前目录下放上自己表白的图片
imgLable = Label(window,image=photo)
# columnspan 组件所跨越的列数
imgLable.grid(row=2,columnspan=2)
def close_all_window():
window.destroy()
def love():
# 是一个独立的顶级窗口
love = Toplevel(window)
love.geometry('300x150+500+260')
love.title('太好了!')
lable = Label(love,text='好巧,我也是',font=('微软雅黑',30))
lable.pack()
btn = Button(love,text='确定',width=10,height=2,command=close_all_window)
btn.pack()
def closed_dislove():
pass
def dis_love():
# 是一个独立的顶级窗口
dislove = Toplevel(window)
dislove.geometry('300x150+500+260')
dislove.title("再考虑考虑呗")
lable = Label(dislove, text='再考虑考虑呗', font=('微软雅黑', 30))
lable.pack()
btn = Button(dislove, text='确定', width=10, height=2,command=dislove.destroy)
btn.pack()
dislove.protocol('WM_DELETE_WINDOW',closed_dislove)
# 按钮
btn1 = Button(window,text='喜欢',width=15,height=2,command=love)
btn1.grid(row=3,column=0,sticky=W)
btn2 = Button(window,text='不喜欢',command=dis_love)
btn2.grid(row=3,column=1,sticky=E)
# 显示窗口
window.mainloop()