基于Python編寫一個爆炸信息窗口腳本
前言
Hello!大家好,有好幾天沒有跟大家見面咯~不知道大家是否在等待《小玩意兒》專欄的更新呢
上一篇的文章【老師見打系列】:我只是寫了一個自動回復(fù)討論的腳本~
感覺挺受大伙的喜歡的呢,非常感謝各位兄弟給哥們頂上熱榜,你們的支持就是我更新的動力
所以這幾天我就在想是否繼續(xù)往【老師見打系列】更新文章,想出一些能讓”老師見打“的idear,當(dāng)然,我并不是要故意惹老師生氣的哈……
直到前天,突然想寫點什么,于是打開了pycharm,當(dāng)我正在想著還有什么好的idear的時候,突然看到了左下角的一個py文件,”爆炸信息.py“我啥時候?qū)懙模磕菚r的我一臉懵逼的看著文件里的代碼,腦海里沒有任何有關(guān)于它的記憶,不會是哥們夢游的時候給敲的吧……
仔細把代碼看了個遍,突然腦海里才回想起在某年某月某日閑著沒事干瞎寫了這么一個代碼,當(dāng)我再次運行的時候,彈出了一個在我看來丑……呃呃呃那個……界面不太好看的窗口,于是點擊窗口發(fā)送信息……欸呀我滴媽?。。∵@這這,運行個啥,代碼太短,功能太少,界面還丑八怪咿呀咿呀~
說時遲那時快,突然冒出一個idear,為啥不把這個代碼完善一下呢?搞個好看整潔一點的界面,到時候再寫一篇文章,文章名我都想好了,你看那標(biāo)題,起得這么熟練!咱們說干就干
于是哥們又挺著千年老腰,花了兩三個小時的時間敲出一個還算不錯的代碼,用了一下,效果很好,我兄弟把我好友給刪了……
開個玩笑,好了,具體實現(xiàn)效果如何,請各位看官往下看
爆炸信息窗口
適當(dāng)娛樂,請勿打擾他人正常生活哦
設(shè)計思路
我是這樣想的,如果只簡單的寫一個單一信息發(fā)送就太無趣了,于是我就像每次得發(fā)不同的話,那才有意思,于是就想到了txt文件,但是,只有文字是不是效果不強,于是想到了下最流行的交流方式:表情包,誒~這就齊了(當(dāng)然各位還有其他的idear可以在評論區(qū)里評論哈)
模塊準(zhǔn)備
tkinter?不行,窗口界面差點意思,那就用 ttkbootstrap模塊,ttkbootstrap 是一個基于 tkinter 的界面美化庫,使用這個工具可以開發(fā)出類似前端 bootstrap 風(fēng)格的 tkinter 桌面程序,但是還是得用到tkinter中的文件讀取函數(shù)
pynput模塊,pynput是一個監(jiān)聽、操作鍵盤鼠標(biāo)庫,主要用來實現(xiàn)消息的發(fā)送,咱們的招數(shù)能不能放出去就靠它了
time模塊,time用來控制放招的間隔,咱們得講究攻守進退呀!
ctypes模塊,
os模塊,主要用來查找文件,配合ttkbootstrap,擦出大火花
PIL模塊,可以叫它圖像處理工具包,用來讀取表情包
win32con模塊,此模塊與pywin32配合使用,模塊中定義了Windows下關(guān)于圖形操作的API
win32clipboard模塊,它的功能主要有剪切中文、圖片等信息,表情包能不能發(fā)出去就靠它了
pyautogui模塊,主要用來控制按下鍵盤的Ctrl+V鍵,相信各位對Ctrl C 和 Ctrl V的操作相當(dāng)之熟練了吧
刪除好友警告
先給大家開一下演示結(jié)果,帶不帶勁
以下高能,娛樂有度?? 請勿頻繁使用 ?? 刪除好友警告
第一式:只說一句

第二式:唐僧念經(jīng)(發(fā)送txt文件中的內(nèi)容)

第三式:不說了,扔圖(發(fā)送表情包)

源代碼
from tkinter import filedialog, Tk, Label
import ttkbootstrap as tk
from ttkbootstrap.constants import *
from pynput.keyboard import Key, Controller as key
from pynput.mouse import Button as Bu, Controller as mouse_el
import time
from ctypes import *
import os
from PIL import Image
import win32con, win32clipboard
import pyautogui
"""
============= 窗口設(shè)計 ===========
"""
class Explosion_window(tk.Frame):
# master等待接收根窗口對象,app等待接收自定義的模塊
def __init__(self, master=None, app=None):
tk.Frame.__init__(self, master)
self.master = master
self.pack()
# 調(diào)用在根窗口創(chuàng)建組件的函數(shù)
self.createWidget()
def createWidget(self):
tk.Label(self, text='IT工藤新一 爆炸信息窗口', font=('華文行楷', 25)).grid(row=0, column=0, pady=10)
tk.Button(self, text='第一式:我只說一句', command=self.first_formula, bootstyle=SUCCESS).grid(row=1, column=0, pady=10)
tk.Button(self, text='第二式:唐僧念經(jīng)', command=self.second_formula, bootstyle=SUCCESS).grid(row=2, column=0, pady=10)
tk.Button(self, text='第三式:不說了,扔圖', command=self.three_formula, bootstyle=SUCCESS).grid(row=3, column=0, pady=10)
# 第一式
def first_formula(self):
root_1 = tk.Toplevel() # 實例化一個頂級類窗口
root_1.title('第一式')
root_1.geometry('500x300')
tk.Label(root_1, text='第一式:我只說一句', font=('華文行楷', 22)).grid(row=0, column=1, pady=10)
tk.Label(root_1, text='招數(shù)內(nèi)容:', font=('華文行楷', 15)).grid(row=1,column=0)
tk.Label(root_1, text='放招次數(shù):', font=('華文行楷', 15)).grid(row=2, column=0)
tk.Label(root_1, text='放招間隔(s):', font=('華文行楷', 15)).grid(row=3, column=0)
words = tk.StringVar() # 接收用戶輸入的文字
times = tk.IntVar() # 結(jié)束放招次數(shù)
time_interval = tk.IntVar() # 接收放招間隔
tk.Entry(root_1, textvariable=words, font=('黑體', 15)).grid(row=1, column=1, pady=10)
tk.Entry(root_1, textvariable=times, font=('黑體', 15)).grid(row=2, column=1, pady=10)
tk.Entry(root_1, textvariable=time_interval, font=('黑體', 15)).grid(row=3, column=1, pady=10)
tk.Button(root_1, text='確定放招', command=lambda: app.first_move(words.get(), times.get(), time_interval.get(), root_1), bootstyle=(SUCCESS, OUTLINE)).grid(row=4, column=1)
root_1.mainloop()
def second_formula(self):
formula = 2
self.second_three_formula(formula) # 調(diào)用放招函數(shù)
def three_formula(self):
formula = 3
self.second_three_formula(formula) # 調(diào)用放招函數(shù)
def second_three_formula(self, formula): # 第二、第三式的窗口一樣,為了提高代碼的重用性,用選擇判斷語句進行操作
root_2 = tk.Toplevel()
if formula == 2:
title1 = '第二式:唐僧念經(jīng)'
text1 = '選擇txt文件'
tk.Button(root_2, text='確定放招', command=lambda: app.second_move(filepath.get(), time_interval.get(), root_2),
bootstyle=(SUCCESS, OUTLINE)).grid(row=3, column=1, pady=10)
elif formula == 3:
title1 = '第三式:不說了,扔圖'
text1 = '選擇表情包文件夾'
tk.Button(root_2, text='確定放招', command=lambda: app.three_move(filepath.get(), time_interval.get(), root_2),
bootstyle=(SUCCESS, OUTLINE)).grid(row=3, column=1, pady=10)
root_2.title(title1)
root_2.geometry('700x190')
filepath = tk.StringVar() # 接收路徑
time_interval = tk.IntVar() # 接收放招間隔
tk.Label(root_2, text=title1, font=('華文行楷', 22)).grid(row=0, column=1)
tk.Label(root_2, text='文件路徑:', font=('華文行楷', 15)).grid(row=1, column=0)
tk.Label(root_2, text='放招間隔(s):', font=('華文行楷', 15)).grid(row=2, column=0)
def select_file(filepath): # 選擇
# 選擇文件夾
if formula == 2:
select_file_path = filedialog.askopenfilename() # 使用askopenfilename函數(shù)選擇單個文件
elif formula == 3:
select_file_path = filedialog.askdirectory() # askdirectory選擇文件夾
filepath.set(select_file_path)
tk.Entry(root_2, textvariable=filepath, font=('黑體', 15)).grid(row=1, column=1)
tk.Entry(root_2, textvariable=time_interval, font=('黑體', 15)).grid(row=2, column=1)
tk.Button(root_2, text=text1, command=lambda: select_file(filepath)).grid(row=1, column=2, pady=10)
# 倒計時
def count_down(self, info=None):
pass
"""
=============== 功能實現(xiàn) ================
"""
class Send_information(object):
def __init__(self):
pass
# 放招函數(shù),用于第一、二式
def Release_moves(self, words=None, times=0, time_interval=0, txt_li=None): # times接收發(fā)送的次數(shù),time_interval接收發(fā)送的時間間隔
keyboard = key() # 獲取鍵盤權(quán)限
mouse = mouse_el() # 獲取鼠標(biāo)權(quán)限
mouse.press(Bu.left) # 鼠標(biāo)左鍵點擊
mouse.release(Bu.left) # 鼠標(biāo)左鍵松開
n = 5
print('請在五秒內(nèi)將鼠標(biāo)放到聊天框內(nèi)并點擊?。?!')
for k in range(5):
print(f'倒計時{n - k}秒')
time.sleep(1) # 程序運行等待五秒你是豬
if times == 0:
for i in txt_li:
keyboard.type(f"{i}") # 輸入框的內(nèi)容
keyboard.press(Key.enter) # 回車鍵按下
keyboard.release(Key.enter) # 回車鍵松開
time.sleep(time_interval)
else:
for i in range(times):
keyboard.type(f"{words}") # 輸入框的內(nèi)容
keyboard.press(Key.enter) # 回車鍵按下
keyboard.release(Key.enter) # 回車鍵松開
time.sleep(time_interval)
# 接收用戶輸入的數(shù)據(jù)
def first_move(self, words, times, time_interval, root_1):
root_1.destroy()
self.Release_moves(words=words, times=times, time_interval=time_interval) # 調(diào)用放招函數(shù)
# 接收用戶選擇的文件路徑
def second_move(self, txt_path, time_interval, root_2):
root_2.destroy()
txt_li = [] # 保存txt中的語句
with open(txt_path, 'r', encoding='utf-8') as f:
for line in f: # 循環(huán)遍歷輸出txt文件內(nèi)容
if line in ['\n', '\r\n']: # 判空處理
pass
elif line.strip() == "": # 空行直接跳過
pass
else:
txt_li.append(line.strip()) # 將內(nèi)容保存到txt中
self.Release_moves(txt_li=txt_li, time_interval=time_interval) # 調(diào)用放招哈數(shù)
# 接收用戶選擇的圖片
def three_move(self, photo_path, time_interval, root_3):
root_3.destroy()
filepath = [photo_path + '/' + file for file in os.listdir(photo_path)] # 拼接路徑
i = 0
n = 5
print('請在五秒內(nèi)將鼠標(biāo)放到聊天框內(nèi)并點擊?。?!')
for k in range(5):
print(f'倒計時{n - k}秒')
time.sleep(1) # 程序運行等待五秒你是豬
# 復(fù)制圖片
for path in filepath:
try:
im = Image.open(path)
im.save('11.bmp')
aString = windll.user32.LoadImageW(0, r"11.bmp", win32con.IMAGE_BITMAP, 0, 0, win32con.LR_LOADFROMFILE)
except:
continue
if aString != 0: ## 由于圖片編碼問題 圖片載入失敗的話 aString 就等于0
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(win32con.CF_BITMAP, aString)
win32clipboard.CloseClipboard()
keyboard = key() # 獲取鍵盤權(quán)限
if i == 0:
i += 0
pyautogui.hotkey('ctrl', 'v')
keyboard.press(Key.enter) # 回車鍵按下
keyboard.release(Key.enter) # 回車鍵松開
time.sleep(time_interval)
if __name__ == '__main__':
root = tk.Window() # 建立一個根窗口
root.title('爆炸信息') # 窗口名稱
root.geometry('500x300') # 窗口大小 寬x高
app = Send_information() # 實例化Send_information對象
Explosion_window(root, app) # 實例化Explosion_window對象
root.mainloop()
這時你可能會問
誒,到了這的小伙伴可能會有些疑問:表情包怎么下載呀?一個個手動下載豈不是太麻煩了呀……
別擔(dān)心,我怎么會讓你們動手呢,最多動動腦
來來來!敲黑板了!??!現(xiàn)在布置一個作業(yè)!接下來我將放一個批量下載表情包的源碼,通過觀察源碼進行操作,實現(xiàn)圖片下載。
提醒!真相只有一個:只用改url
批量獲取表情包
import requests
from lxml import etree
import os
import threading
def get_url(url, img_urls): #獲取圖片url
res = requests.get(url, headers=headers) # 發(fā)送請求
html = etree.HTML(res.text) # 將html元素轉(zhuǎn)換成html對象
img_urls += html.xpath('//div[@class="thumbnail"]/a/img/@src')
def user_choose(): # 用戶選擇下載圖片的頁數(shù)
img_urls = [] # 存放圖片url
# 請輸入下載網(wǎng)址
url = 'http://www.bbsnet.com/egao'
# 用戶輸入頁數(shù)
page = int(input('請輸入獲取的頁數(shù):'))
for i in range(0, page):
if page == 0:
get_url(url, img_urls)
elif page >= 1:
link = url + f'/page/{i+1}' # 拼接鏈接
get_url(link, img_urls) #調(diào)用獲取圖片url的函數(shù)
return img_urls
def download_picture(img_url, i, j): # 下載圖片
res = requests.get(img_url, headers)
with open(f'./表情包/表情包-{i}.{j}', 'wb') as f:
f.write(res.content)
if __name__ == '__main__':
if not os.path.exists('./表情包'):
os.makedirs('./表情包')
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
}
img_urls = user_choose()
threads = []
for i in range(len(img_urls)):
t = threading.Thread(target=download_picture, args=(img_urls[i], i, img_urls[i][-3:]))
threads.append(t)
for t in threads:
t.start()
到此這篇關(guān)于基于Python編寫一個爆炸信息窗口腳本的文章就介紹到這了,更多相關(guān)Python爆炸信息窗口內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實現(xiàn)生命游戲的示例代碼(Game of Life)
這篇文章主要介紹了python實現(xiàn)生命游戲的示例代碼(Game of Life),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
Python中通過@classmethod 實現(xiàn)多態(tài)的示例
這篇文章主要介紹了Python中通過@classmethod 實現(xiàn)多態(tài),python中通常使用對象創(chuàng)建多態(tài)模式,python還支持類創(chuàng)建多態(tài)模式,下面通過一個例子展示它如何實現(xiàn)多態(tài),需要的朋友可以參考下2022-11-11
Python+Matplotlib繪制高亮顯示餅圖的示例代碼
餅圖 (Pie Chart) 是一種圓形統(tǒng)計圖,被分割成片用于表示數(shù)值間的比例關(guān)系,本文為大家介紹了Matplotlib繪制高亮顯示的餅圖的函數(shù)源碼,需要的可以參考一下2023-06-06
pandas之關(guān)于DataFrame數(shù)據(jù)類型超好用的方法
這篇文章主要介紹了pandas之關(guān)于DataFrame數(shù)據(jù)類型超好用的方法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
python3.6之xlwt如何設(shè)置單元格對齊方式
這篇文章主要介紹了python3.6之xlwt如何設(shè)置單元格對齊方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06

