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
'''
难点
按键复用
'''
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() # 获取显示区域的宽度
screenHeight = self.window.winfo_screenheight() # 获取显示区域的高度
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]) # 括号里为需要显示在图形化界面里的图片
photo = photo.resize((150, 50)) # 规定图片大小
self.paned.img = ImageTk.PhotoImage(photo)
self.l0 = Label(self.window, image=self.paned.img, justify='center')
self.l1 = ttk.Label(self.window, text='内容:')
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='关于',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='说明',command=self.show_infos)
#设置热键
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内容复制到剪切板
pyperclip.copy(t1_content)
keyboard = Controller()
self.b1.config(text='停止')
self.t1.config(state='disable')
while True:
# 使用control+v组合键进行粘贴
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('错误', '请输入正确的数值!')
self.c1.delete(0, END)
except ValueError:
messagebox.showerror('错误', '请输入正确的数值!')
self.c1.delete(0, END)
else:
messagebox.showerror('错误', '还没有输入内容')
else:
self.flag=False
self.b1.config(text='开始')
def thread_it(self,func,*args):
t=threading.Thread(target=func,args=args)
t.setDaemon(True)#设置守护线程,即主线程结束,子线程也结束
t.start()
def show_infos(self):
messagebox.showinfo('说明','***本软件完全免费***\n\n1.输入刷屏内容\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()
到此这篇关于python3 GUI刷屏器(附源码)的文章就介绍到这了,更多相关python刷屏器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!