这个版本其实网上也有很多,应我的直播间盟主假爱情邀请,我这也给大家写一个
版本说明,和之前的整蛊弹窗一版本的区别就是按钮没有对应的方法变了
版本1:鼠标移到按钮没有时,直接会跑开
版本S:鼠标点击按钮没有时,切换图片进行提醒
这里为了省事,就直接在我之前的整蛊弹窗一版本的代码的基础上写吧
整蛊弹窗一版本:https://blog.csdn.net/python1639er/article/details/104124462
1 修改版本一种按钮没有对应的方法
实际上只需要把一版本里面的没有按钮改下对应的方法
使得每点击一次这个没有按钮图片会换一张
这里首先要获取图片列表,并把图片列表的第一张图展示出来
把版本1里面的imglabel相关的代码修改成如下
# 这里的图片文件夹路径对于小白来说,最保险是绝对路径
# 如果你的图片和代码在一个文件夹,直接可以使用下面一行
# IMGPATH = "./"
# 如果代码文件和图片所在文件夹在同一目录,即代码和img文件夹在同一目录,直接用下一行
# IMGPATH = "img/"
# 我的github里,代码文件夹和图片文件夹在同一目录,所以使用的是下一行
IMGROOT = "../img/"
def get_img_list(img_root):
img_list = [os.path.join(img_root, img_path) for img_path in os.listdir(img_root) if os.path.splitext(img_path)[1] in ['.gif']]
return [tk.PhotoImage(file=img_p) for img_p in img_list]
count = 0
photoList = get_img_list(IMGROOT)
imgLabel = tk.Label(win, image=photoList[count])
imgLabel.place(x=img_x, y=img_y)
然后每点击一次没有,我们就切换成下一张图,那么添加点击按钮没有的方法并绑定给按钮
# 按钮
def clickno():
global count
count += 1
imgLabel.config(image=photoList[count % len(photoList)])
no_button = tk.Button(win, text=no, command=clickno)
并删除掉原有的绑定mouse_in_no
方法的代码,到这里这个版本就算完成了
点击没有按钮会切换img文件夹里面的图片
本阶段对应的最后代码如下
#usr/bin/env python
#-*- coding:utf-8- -*-
import tkinter as tk
import tkinter.font as tkFont # 引入字体模块
import time
import random
import os
WINWIDTH = 800
WINHEIGHT = 600
WINX = 400
WINY = 100
img_x = 180
img_y = 60
question_y = 20
button_width = 100
button_height = 40
yes_button_x = img_x - button_width // 2
no_button_x = WINWIDTH - img_x - button_width//2
button_y = 520
question = "我喜欢你,有机会吗?"
yes = "有"
no = "没有"
title = "大妹子"
# 新建无法直接关闭的TK类
class NewTk(tk.Tk):
def destroy(self):
# 点击界面右上角的关闭按钮时,会调用本函数,
# 覆盖掉了父类的关闭方法,使得界面无法关闭
pass
win = NewTk()
win.title(title)
win.geometry("%sx%s+%s+%s" % (WINWIDTH, WINHEIGHT, WINX, WINY))
IMGROOT = "../img/"
def get_img_list(img_root):
img_list = [os.path.join(img_root, img_path) for img_path in os.listdir(img_root) if os.path.splitext(img_path)[1] in ['.gif']]
return [tk.PhotoImage(file=img_p) for img_p in img_list]
count = 0
photoList = get_img_list(IMGROOT)
imgLabel = tk.Label(win, image=photoList[count])
imgLabel.place(x=img_x, y=img_y)
quesft = tkFont.Font(family="微软雅黑", size=16, weight=tkFont.BOLD)
q = tk.Label(win, text=question, font=quesft)
q.place(x=img_x, y=question_y)
# 按钮
def clickyes():
yes_reply = "(*/ω\*)"
top_width = 100
top_height = 50
top = tk.Toplevel()
# 设置弹出窗口位置和大小
top_x = WINX+WINWIDTH//2-top_width//2
top_y = WINY+WINHEIGHT//2-top_height//2
top_loc = "{}x{}+{}+{}".format(top_width, top_height, top_x, top_y)
top.geometry(top_loc)
# 添加内容
tk.Label(top, text = yes_reply).pack()
win.update()
time.sleep(1)
# 关闭程序
exit()
yes_button = tk.Button(win, text=yes, command=clickyes)
yes_button.place(x=yes_button_x, y=button_y, width=button_width, height=button_height)
# 按钮
def clickno():
global count
count += 1
imgLabel.config(image=photoList[count % len(photoList)])
no_button = tk.Button(win, text=no, command=clickno)
no_button.place(x=no_button_x, y=button_y, width=button_width, height=button_height)
win.mainloop()
2 优化并打包
由于盟主假爱情并没有安装python,可能也有很多小伙伴和他一样没有安装python,所以这里打包成exe给大家
TODO