Senior artists. I have a question about the Python Tkinter button stopping.

Asked 2 years ago, Updated 2 years ago, 85 views

Hello.

I'm a beginner who is studying Python.

I'm just making a program that takes a simple screen shot using Python Tkinter.

I'm asking the seniors like this because there's a stop phenomenon with the following contents.

The sequence of program actions is as follows.

Window -> Click Start Button -> Watch mouse wheel buttons -> Take screenshot when clicking mouse wheel buttons -> Stopping

(The way I want to implement it is simply to press the Start button and then continue to take screenshots whenever the mouse wheel button is pressed.)

Below is the source code.

import pyautogui
import time  
import mouse 
import threading
from tkinter import *


# Creating a window window and adjusting the button screen
root = Tk() 
root.title ("Taking Wheel Button SC") 
root.geometry("540x480+100+100")
root.resizable(False,FALSE)


# Press Start button to monitor mouse wheel button ->Take a screenshot when clicking the wheel button
def btnscreen(): 
    while True: 
        if mouse.is_pressed("middle"):
            pyautogui.hotkey('win','prtscr')

        if pyautogui.press('z'):
            break    


# Create Start Button
btn6 = Button (root,text='start button', command=btnscreen) 
btn6.pack()

# Keep windows open
root.mainloop()

(After googling, it says to implement threads and timers, but I don't know how to apply them to the current code.)

That's it. Sunbae, please answer. Thank you.

python tkinter gui

2022-09-20 17:59

2 Answers

You can use the threading module.

import threading
def th():
    th = threading.Thread(target=btnscreen)
    th.daemon = True
    th.start()

def btnscreen(): 
    while True:
     if mouse.is_pressed("middle"): 
        pyautogui.hotkey('win','prtscr')

     if pyautogui.press('z'):
        break
btn6 = Button (root,text='start button',command=th)
btn6.pack()


2022-09-20 17:59

I don't know much about tkinter. I'm not good enough. I'm not good enough with questions. I'm leaving a message just in case.

Does it mean that the program stops? Does it mean that the computer is going to stop?

I'm afraid the computer will stop, so I can't run the code.

From the code, it seems that btnscreen() works when you click the mouse wheel. I think there's a problem with that part.

def btnscreen(): 
    while True: 
        if mouse.is_pressed("middle"):
            pyautogui.hotkey('win','prtscr')
        if pyautogui.press('z'):
            break
    """
    Break does not work unless the z key is pressed, so it looks like an infinite loop.

    In other words, if there is no additional manipulation,
    while True:
        pass
    Because it is in the same state as , the cpu load is expected during the operation.
    """

I think it should be modified as follows.

def btnscreen(): 
    if mouse.is_pressed("middle"):
        pyautogui.hotkey('win','prtscr')


2022-09-20 17:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.