過(guò)年GUI博客二連發(fā),本打算出去玩玩,奈何空氣,天氣實(shí)在差,遂使用tkinter開發(fā)一款GUI刷屏器,寫此博客記錄一下我的開發(fā)思路。
import re
import time
import pyperclip
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
from pynput.keyboard import Key, Controller
import threading
from PIL import Image ,ImageTk
'''
難點(diǎn)
按鍵復(fù)用
'''
imgs=["./rely/logo.png",'./rely/favicon.ico']
class App:
def __init__(self):
self.flag=True
self.window = Tk()
width = 230
height = 260
screenWidth = self.window.winfo_screenwidth() # 獲取顯示區(qū)域的寬度
screenHeight = self.window.winfo_screenheight() # 獲取顯示區(qū)域的高度
left = (screenWidth - width) / 2
top = (screenHeight - height) / 2
self.window.geometry("%dx%d+%d+%d" % (width, height, left, top))
self.window.title('刷一刷-v1.0')
self.window.iconbitmap(imgs[1])
self.window.resizable(0, 0)
self.create_widget()
self.config_widget()
self.place_widget()
self.window.mainloop()
def create_widget(self):
self.paned=PanedWindow(self.window)
self.img=imgs
photo = Image.open(self.img[0]) # 括號(hào)里為需要顯示在圖形化界面里的圖片
photo = photo.resize((150, 50)) # 規(guī)定圖片大小
self.paned.img = ImageTk.PhotoImage(photo)
self.l0 = Label(self.window, image=self.paned.img, justify='center')
self.l1 = ttk.Label(self.window, text='內(nèi)容:')
self.l1 = ttk.Label(self.window, text='頻率:')
self.t1 = Text(self.window)
self.c1 = ttk.Combobox(self.window, width=13)
self.l2=ttk.Label(self.window,text='秒/次')
self.b1 = ttk.Button(self.window, text='開始', )
self.b2 = ttk.Button(self.window, text='退出',)
self.m=Menu(self.window)
self.window['menu']=self.m
self.s1=Menu(self.m,tearoff=False)
self.s2=Menu(self.m,tearoff=False)
self.s3=Menu(self.m,tearoff=False)
def place_widget(self):
self.l0.pack()
self.l1.place(x=20, y=90)
self.t1.place(x=40, y=60, width=150, height=80)
self.l1.place(x=20, y=162)
self.c1.place(x=65, y=160,width=80)
self.l2.place(x=160,y=160)
self.b1.place(x=20, y=200)
self.b2.place(x=125, y=200)
def config_widget(self):
self.b1.config(command=lambda: self.thread_it(self.start))
self.b2.config( command=self.window_quit)
rate_list=['1','0.1','0.01']
self.c1.config(value=rate_list)
self.m.add_cascade(label='文件',menu=self.s1)
self.s1.add_command(label='退出',command=self.window_quit)
self.m.add_cascade(label='操作',menu=self.s2)
self.m.add_cascade(label='關(guān)于',menu=self.s3)
self.s2.add_command(label='開始 F9',command=lambda: self.thread_it(self.start))
self.s2.add_command(label='停止 F10',command=lambda: self.thread_it(self.start))
self.s3.add_command(label='說(shuō)明',command=self.show_infos)
#設(shè)置熱鍵
self.window.bind('F9>',lambda: self.thread_it(self.pre_start))
self.window.bind('F10>',lambda: self.thread_it(self.pre_start))
self.window.bind('Escape>',self.escape)
self.window.bind('FocusIn>',self.clear_content)
self.window.protocol('WM_DELETE_WINDOW',self.window_quit)
def clear_content(self,event):
self.t1.delete(0.0,END)
def pre_start(self,event):
self.start()
def start(self):
if self.b1['text']=='開始':
self.flag=True
t1_content = self.t1.get(1.0, 'end').strip()
if len(t1_content) != 0:
gap = self.c1.get()
try:
if re.match('(^0|^1)\.{0,1}\d+$', gap) or int(gap) > 0:
# 將t1內(nèi)容復(fù)制到剪切板
pyperclip.copy(t1_content)
keyboard = Controller()
self.b1.config(text='停止')
self.t1.config(state='disable')
while True:
# 使用control+v組合鍵進(jìn)行粘貼
if self.flag:
keyboard.press(Key.ctrl.value)
keyboard.press('v')
keyboard.release('v')
keyboard.release(Key.ctrl.value)
keyboard.press(Key.enter.value)
keyboard.release(Key.enter.value)
print(t1_content)
time.sleep(float(gap))
else:
break
else:
messagebox.showerror('錯(cuò)誤', '請(qǐng)輸入正確的數(shù)值!')
self.c1.delete(0, END)
except ValueError:
messagebox.showerror('錯(cuò)誤', '請(qǐng)輸入正確的數(shù)值!')
self.c1.delete(0, END)
else:
messagebox.showerror('錯(cuò)誤', '還沒有輸入內(nèi)容')
else:
self.flag=False
self.b1.config(text='開始')
def thread_it(self,func,*args):
t=threading.Thread(target=func,args=args)
t.setDaemon(True)#設(shè)置守護(hù)線程,即主線程結(jié)束,子線程也結(jié)束
t.start()
def show_infos(self):
messagebox.showinfo('說(shuō)明','***本軟件完全免費(fèi)***\n\n1.輸入刷屏內(nèi)容\n2.選擇(輸入)刷屏頻率\n3.開始(F9)刷屏\n4.停止(F10)刷屏')
def window_quit(self):
ret=messagebox.askyesno('退出','是否要退出?')
if ret:
self.window.destroy()
def escape(self,event):
self.window_quit()
if __name__ == '__main__':
a=App()
本次使用tkinter寫了一款刷屏器,能夠?qū)崿F(xiàn)短時(shí)間內(nèi)相同文本的發(fā)送,繼而實(shí)現(xiàn)刷屏的目的。在代碼的撰寫上,模擬鍵盤輸入主要參考了:
到此這篇關(guān)于python3 GUI刷屏器(附源碼)的文章就介紹到這了,更多相關(guān)python刷屏器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!