佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > 詳解python3 GUI刷屏器(附源碼)

詳解python3 GUI刷屏器(附源碼)

熱門標(biāo)簽:硅谷的囚徒呼叫中心 Win7旗艦版 客戶服務(wù) 呼叫中心市場(chǎng)需求 電話運(yùn)營中心 企業(yè)做大做強(qiáng) 語音系統(tǒng) 百度AI接口

過年GUI博客二連發(fā),本打算出去玩玩,奈何空氣,天氣實(shí)在差,遂使用tkinter開發(fā)一款GUI刷屏器,寫此博客記錄一下我的開發(fā)思路。

一.準(zhǔn)備工作

本次使用除tkinter庫之外還使用了pynput庫,可以使用

pip install pynput

安裝

二.預(yù)覽

在長文本框中輸入要刷屏的內(nèi)容,通過設(shè)置刷屏頻率(單位:秒)即可實(shí)現(xiàn)刷屏。

三.設(shè)計(jì)流程

四.源代碼

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='說明',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('說明','***本軟件完全免費(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()

五.總結(jié)

本次使用tkinter寫了一款刷屏器,能夠?qū)崿F(xiàn)短時(shí)間內(nèi)相同文本的發(fā)送,繼而實(shí)現(xiàn)刷屏的目的。在代碼的撰寫上,模擬鍵盤輸入主要參考了:

python模擬鼠標(biāo)點(diǎn)擊和鍵盤輸入的操作

實(shí)現(xiàn)了組合鍵Ctrl+V的操作。本篇技術(shù)含量不多,重點(diǎn)在代碼邏輯思路上。

到此這篇關(guān)于python3 GUI刷屏器(附源碼)的文章就介紹到這了,更多相關(guān)python刷屏器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 詳解Python GUI編程之PyQt5入門到實(shí)戰(zhàn)
  • 詳解Python中pyautogui庫的最全使用方法
  • python GUI庫圖形界面開發(fā)之PyQt5簡單繪圖板實(shí)例與代碼分析
  • python GUI庫圖形界面開發(fā)之PyQt5信號(hào)與槽多窗口數(shù)據(jù)傳遞詳細(xì)使用方法與實(shí)例

標(biāo)簽:山西 喀什 長沙 安康 海南 濟(jì)南 山西 崇左

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解python3 GUI刷屏器(附源碼)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    惠水县| 博客| 西畴县| 抚顺市| 清镇市| 娄底市| 洛川县| 兴义市| 三门峡市| 剑阁县| 象山县| 磐安县| 阿坝| 怀集县| 于都县| 融水| 莆田市| 吴桥县| 隆德县| 邓州市| 丽江市| 改则县| 灵台县| 夏邑县| 青州市| 确山县| 荆门市| 龙游县| 怀来县| 三江| 措勤县| 哈巴河县| 岐山县| 雷州市| 甘南县| 台东市| 平武县| 康保县| 龙南县| 合水县| 收藏|