python tkinter從入門到撩妹-整蠱彈窗-S特供版

這個版本其實網上也有很多,應我的直播間盟主假愛情邀請,我這也給大家寫一個
版本說明,和之前的整蠱彈窗一版本的區別就是按鈕沒有對應的方法變了
版本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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章