Working with tkinter and mouse

Background

・ I use temas in my company. If you do not operate the PC for 15 minutes or more, the status will be left. That's a problem. ・ Sometimes I want to take a break. ・ I was able to operate the mouse on the screen for the time being, but there may be various bugs. ・ Because I am a beginner in programming, various things do not go well. Great person to help.

By the way, even if I moved the mouse with the script, the status of teams was absent. Remorse ...

Real

After pressing the start button,

    1. The mouse moves randomly for the specified time.
  1. Open Notepad and enter the appropriate character string on the keyboard. (Keyboard monitoring measures. During maintenance)
    1. Get the latest news. (Traffic monitoring measures. During maintenance) Image.png

code

working.py


import tkinter as tk
import pyautogui as pgui
import random
import datetime
import time
import subprocess
import string
import requests

class Application(tk.Frame):
    def __init__(self, master):
        super().__init__(master)

        master.geometry("230x130")
        master.title("Working!")

        self.create_widgets()

    def create_widgets(self):
        self.grid(column=0, row=0, sticky=tk.NSEW, padx=5, pady=5)
        self.time_label = tk.Label(self, text="Time designation(Minutes)").grid(column=0, row=0, pady=10)
        self.time_box = tk.Entry(self)
        self.time_box.insert(0, '60')
        self.time_box.grid(column=1, row=0, sticky=tk.EW, padx=3)
        self.work_label = tk.Label(self, text="Today's work").grid(column=0, row=1, pady=10)
        self.status = tk.Label(self, text="Get serious from now on")
        self.status.grid(column=1, row=1, sticky=tk.EW, padx=3)
        self.start_btn = tk.Button(self, text="start", command=self.we_love_work).grid(column=1, row=2)

    #Button action
    def we_love_work(self):
        #action_flg = random.randint(1, 3)
        action_flg = 1
        self.status.config(text="Mouse messing around") #Will not update

        time_box_value = self.time_box.get()
        end_time = datetime.datetime.now() + datetime.timedelta(minutes=int(time_box_value))
        while True:
            if datetime.datetime.now() >= end_time:
               break

            if action_flg == 1:
                self.do_mouse()
            elif action_flg == 2:
                self.do_keyboard()
            elif action_flg == 3:
                self.do_news()

    def do_mouse(self):
        window_x = 1366
        window_y = 768
        for i in range(50):
            point_x = random.randrange(0, window_x)
            point_y = random.randrange(0, window_y)
            pgui.moveTo(point_x, point_y, duration=1)
            time.sleep(3)

    def do_keyboard(self):
        #I can operate the keyboard, but I can't write well in the memo
        process = subprocess.Popen(r'C:\Windows\System32\notepad.exe')
        pgui.click(50, 50)
        for i in range(10):
            pgui.typewrite(self.random_string(100))
            pgui.typewrite('\r\n')
            time.sleep(3)
        process.kill()

    def do_news(self):
        keyword = "latest"
        url = 'https://news.google.com/search'
        params = {'hl': 'ja', 'gl': 'JP', 'ceid': 'JP:ja', 'q': keyword}
        res = requests.get(url, params=params)
        print(res.text)
        time.sleep(60 * 3) #Maybe it's better to leave a proper interval?

    def random_string(n):
        return ''.join(random.choices(string.ascii_letters + string.digits, k=n))

def main():
    win = tk.Tk()
    app = Application(master=win)
    app.mainloop()

if __name__ == "__main__":
    main()

Recommended Posts

Working with tkinter and mouse
Programming with Python and Tkinter
Async / await with Kivy and tkinter
[Automation] Manipulate mouse and keyboard with Python
MVC with Tkinter
Become Santa with Tkinter
With and without WSGI
Create a native GUI app with Py2app and Tkinter
Let's make a Mac app with Tkinter and py2app
With me, cp, and Subprocess
Encryption and decryption with Python
Getting Started with Tkinter 2: Buttons
Python and hardware-Using RS232C with Python-
Working with LibreOffice in Python
Python: Working with Firefox with selenium
Working with sounds in Python
Screen switching / screen transition with Tkinter
[Python] Theremin interface with mouse
Create Image Viewer with Tkinter
Let's make dice with tkinter
Problems with `visudo` not working on Ubuntu 18.04 and its workaround
Run Label with tkinter [Python]
Super-resolution with SRGAN and ESRGAN
group_by with sqlalchemy and sum
python with pyenv and venv
I measured BMI with tkinter
I tried to make GUI tic-tac-toe with Python and Tkinter
With me, NER and Flair
Works with Python and R
Communicate with FX-5204PS with Python and PyUSB
Interactive visualization with ipywidgets and Bokeh
Shining life with Python and OpenCV
Python-Mouse and keyboard operation with pyautogui
Sorting with mixed numbers and letters
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
Working with LibreOffice in Python: import
Easy Slackbot with Docker and Errbot
Image segmentation with scikit-image and scikit-learn
Authentication process with gRPC and Firebase Authentication
Scraping with Python, Selenium and Chromedriver
Play with Poincare series and SymPy
HTTPS with Django and Let's Encrypt
Photo segmentation and clustering with DBSCAN
Scraping with Python and Beautiful Soup
NAS backup with php and rsync
JSON encoding and decoding with python
Easy GUI app with Tkinter Text
Path processing with takewhile and dropwhile
Basic authentication and Digest authentication with Flask
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
Compare DCGAN and pix2pix with keras
Using Sessions and Reflections with SQLAlchemy
Reading and writing NetCDF with Python
Introduce errBot and work with Slack
Save and retrieve files with Pepper
I played with PyQt5 and Python3