过年gui博客二连发,本打算出去玩玩,奈何空气,天气实在差,遂使用tkinter开发一款gui刷屏器,写此博客记录一下我的开发思路。
一.准备工作
本次使用除tkinter库之外还使用了pynput库,可以使用
1
|
pip install pynput
|
安装
二.预览
在长文本框中输入要刷屏的内容,通过设置刷屏频率(单位:秒)即可实现刷屏。
三.设计流程
四.源代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
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()
|
五.总结
本次使用tkinter写了一款刷屏器,能够实现短时间内相同文本的发送,继而实现刷屏的目的。在代码的撰写上,模拟键盘输入主要参考了:
python模拟鼠标点击和键盘输入的操作
实现了组合键ctrl+v的操作。本篇技术含量不多,重点在代码逻辑思路上。
到此这篇关于python3 gui刷屏器(附源码)的文章就介绍到这了,更多相关python刷屏器内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/a1397852386/article/details/113806309