python如何写出表白程序
用python编表白程序的方法:
1、创建GUI窗口,实现代码的调用。
2、编写点击触发函数,实现表白程序。
具体代码如下:
from tkinter import * from tkinter import messagebox def closeWindow(): messagebox.showinfo(title="警告",message="不许关闭,好好回答") # messagebox.showerror(title="警告",message="不许关闭,好好回答") return # 点击喜欢触发的方法 def Love(): # 顶级窗口 love = Toplevel(window) love.geometry("300x100+520+260") love.title("好巧,我也是") label = Label(love,text = "好巧,我也是",font = ("微软雅黑",20)) label.pack() btn = Button(love,text="确定",width=10,height=2,command=closeAllWindow) btn.pack() love.protocol("WM_DELETE_WINDOW",closeLove) def closeLove(): return # 关闭所有的窗口 def closeAllWindow(): # destroy 销毁 window.destroy() def noLove(): no_love = Toplevel(window) no_love.geometry("300x100+520+260") no_love.title("再考虑考虑") label = Label(no_love,text="再考虑考虑呗",font=("微软雅黑",25)) label.pack() btn = Button(no_love,text="好的",width=10,height=2,command=no_love.destroy) btn.pack() no_love.protocol("WM_DELETE_WINDOW",closeNoLove) def closeNoLove(): noLove() # 创建父级窗口 window = Tk() #Tk 是一个类 # 窗口标题 window.title("你喜欢我吗?") # 窗口大小 window.geometry('380x420') # 窗口位置 geometry:几何 window.geometry('+500+240') # protocol() 用户关闭窗口触发的事件 window.protocol("WM_DELETE_WINDOW",closeWindow) # 标签控件 label = Label(window,text="hey,小姐姐",font=("微软雅黑",15),fg='red') # 定位 网格式布局 pack也可以 label.grid(row = 0,column = 0,sticky = W) label1 = Label(window,text="喜欢我吗?",font=("微软雅黑",30)) # sticky 对齐方式 E W S N 东西南北 label1.grid(row = 1,column = 1,sticky = E) # 显示图片 photo = PhotoImage(file = "./cc.png") imageLable = Label(window,image = photo) # columnspan 组件所跨越的列数 imageLable.grid(row = 2, columnspan = 2) # 按钮控件 # command 按钮点击触发的事件 btn = Button(window,text="喜欢",width=15,height=2,command=Love) btn.grid(row = 3,column = 0, sticky = W) btn1 = Button(window,text="不喜欢",command=noLove) btn1.grid(row = 3,column = 1, sticky = E) # 显示窗口,也叫消息循环 window.mainloop()
实例扩展:
复制# 打包操作 # 安装pyinstaller # cmd输入 pip install pyinstaller # shift+右击文件夹 点击在此处打开命令窗口 # pyinstaller -F -w love.py //打包程序 # 引用tkinter工具包 from tkinter import * #__all__=[a,b] #from tkinter import messagebox # 定义关闭窗口提示 def closeWindow(): messagebox.showinfo(title="警告",message ="滚回去,必须选一个") return # 定义喜欢按钮的提示 def Love(): love = Toplevel(window) love.geometry("300x100+250+260") love.title("我也喜欢你") label = Label(love, text = "我也喜欢你!",font = ("微软雅黑",20)) label.pack() btn = Button(love,text = "好呀",width = 10,height = 2,command=closeallwindow) btn.pack() love.protocol("WM_DELETE_WINDOW", closelove) def closelove(): return # 关闭所有窗口 def closeallwindow(): window.destroy() # 定义不喜欢按钮的提示 def noLove(): no_love = Toplevel(window) no_love.geometry("300x100+520+260") no_love.title("重新选") label = Label(no_love,text="乖乖回去重新选!",font = ("微软雅黑",25)) label.pack() btn = Button(no_love,text="好呀", width=10, height=2,command=no_love.destroy) btn.pack() no_love.protocol("WM_DELETE_WINDOW",closenolove) # 窗口不能关 def closenolove(): #messagebox.showinfo("放弃吧回去重选- -") #return noLove() # 创建窗口 window = Tk() # 窗口标题 window.title("喜欢我吗?") # 窗口大小 window.geometry("380x420+500+240") # love.title("哈哈") btn=Button(text="确定") # 窗口位置 # window.geometry("+500+240") window.protocol("WM_DELETE_WINDOW",closeWindow ) #标签控件 label = Label(window,text = "hey,小姐姐",font = ("微软雅黑",15),fg = "red") label.grid(row = 0, column = 0 ) label = Label(window,text = "喜欢我吗?",font = ("微软雅黑",30)) label.grid(row = 1, column = 1,sticky = E) # 插入图片 photo = PhotoImage(file = "./123.png") imageLable = Label(window,image = photo) imageLable.grid(row = 2,columnspan = 2) # 喜欢按钮插件 btn = Button(window,text="喜欢",width=15,height=2,command=Love) btn.grid(row=3,column=0,sticky= W) # 不喜欢按钮插件 btn = Button(window,text="不喜欢" ,command=noLove) btn.grid(row=3,column=1,sticky= E) # 显示窗口 消息循环 window.mainloop()
到此这篇关于python如何写出表白程序的文章就介绍到这了,更多相关如何用python编表白程序内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)