Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed yesterday.
This post was edited and submitted for review 21 hours ago.
Improve this question
i'm working on my first project (bot with image reconition) that have 5 different files, so i decided to make a menu where you start and decide what to load (by import command on selected option) and then when you finish what you have to do you stop it and exit. My problem is i can't figure it out why it won't close, seems like something remain open.
here is option1 (other options are similar):
class AutoClicker(Thread):
clicking = False
def run(self):
while True:
if AutoClicker.clicking:
#here there is some code that do things
sleep(delay * random() + 1/2 * delay)
def keypress(key): # enabler by pressing a defined key
if key == Key.home:
AutoClicker.clicking = not AutoClicker.clicking
if AutoClicker.clicking == True:
print(" Script [ON]\nPress HOME button to stop.")
else:
print(" Script [OFF]\nPress HOME button to start or END button to exit.")
elif key == Key.end:
AutoClicker.clicking == False
exit()
AutoClicker().start()
with Listener(on_press=keypress) as listener:
listener.join()
it works and all that but i can't made it exit, when i press end button it just stops working and clean the window but i have to close it manually. i'm running it by clicking the python file or wuold run it in windows cmd.
thanks to who have the patient to read and try to figure out and help
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 10 days ago.
This post was edited and submitted for review 10 days ago.
Improve this question
I have a request if you don't mind.
My main goal is to code a reorder list items with drag and drop feature in Tkinter (just a simple code example). I prefer it to include different widgets , for example : buttons, images, labels ...
In my case I did an attempt to drag and drop list of buttons but this is not what I want to reach.
If you want to have an idea on how It gonna look like. This is the video which illustrates what I wanna say.
https://youtu.be/LDej-JtE1OY?t=13
This is the code I attempted to make:
import tkinter as tk
def on_button_press(event):
global item_being_dragged
item_being_dragged = event.widget
def on_button_release(event):
global item_being_dragged
item_being_dragged.place_forget()
item_being_dragged.place(x=event.x_root, y=event.y_root)
item_being_dragged = None
root = tk.Tk()
item_being_dragged = None
for i in range(10):
b = tk.Button(root, text=str(i))
b.bind("<ButtonPress-1>", on_button_press)
b.bind("<B1-Motion>", lambda e: item_being_dragged.place(x=e.x_root, y=e.y_root))
b.bind("<ButtonRelease-1>", on_button_release)
b.pack()
I expect (if you can edit my code and turn it to the result shown in the YouTube video link.I will really appreciate that because my goal is just to know how the algorithm of this feature is written and understand it to advance in my journey of learning Tkinter.**
I'm trying to build a program that has 2 inputs connected to 2 limit switches and each of these needs to be first opened and then closed before the function respective to each button begins.
As a safety feature, I would like to block the code until the button is closed again.
After looking around online I found out that the best library is gpiozero but I can't make it work :(
The code gets stuck on the block wait_for_release() and nothing happens when releasing the button.
I don't understand why if I put btn1.wait_for_press() it goes through that but not on wait_for_release.
#btn1.wait_for_press()
#print('If I uncomment wait_for_press it comes here but get stuck on the release anyway')
Is there a way to make this work? Even another method is appreciated!
Doing wait_for_press() and then wait_for_release() doesn't work for me because I need either 1 of the 2 inputs to be pressed.
Thanks in advance, here is the code:
from gpiozero import Button
from signal import pause
btn1 = Button(12, bounce_time=0.2)
btn2= Button(20, bounce_time=0.2)
def release_btn_1():
print('Btn1 pressed')
#btn1.wait_for_press()
#print('Wait for press works')
btn1.wait_for_release()
print('Btn1 released')
#function 1
def release_btn_2():
print('Btn2 pressed')
btn2.wait_for_release()
print('Btn2 released')
#function 2
btn1.when_pressed = release_btn_1
btn2.when_pressed = release_btn_2
pause()
I was hoping someone might have some insight on how to stop a script from continuing to repeat if a button is held (or in my case pressed longer than a second)?
Basically i've a button setup on the breadboard, and I have it coded to play an audio file when the button is pressed. This works, however if the button isn't very quickly tapped, then the audio will repeat itself until button is fully released. Also if the button is pressed and held, the audio file will just repeat indefinitely.
I've recorded a quick recording to demonstrate the issue if its helpful, here: https://streamable.com/esvoy6
I should also note that I am very new to python (coding in general actually), so its most likely something simple that I just haven't been able to find yet. I am using gpiozero for my library.
Any help or insight is greatly appreciated!
Here is what my code looks like right now:
from gpiozero import LED, Button
import vlc
import time
import sys
def sleep_minute(minutes):
sleep(minutes * 60)
# GPIO Pins of Green LED
greenLight = LED(17)
greenButton = Button(27)
# Green Button Pressed Definition
def green_btn_pressed():
print("Green Button Pressed")
greenButton.when_pressed = greenLight.on
greenButton.when_released = greenLight.on
# Executed Script
while True:
if greenButton.is_pressed:
green_btn_pressed()
time.sleep(.1)
print("Game Audio Start")
p = vlc.MediaPlayer("/home/pi/Desktop/10 Second Countdown.mp3")
p.play()
So from a brief look at it, it seems that 'time.sleep(.1)' is not doing what you are expecting. Ie. it is obviously interrupted by button presses. This is not abnormal behaviour as button presses on Ardiuno and raspPi (guessing here) would be processed as interrupts.
The script itself does not contain any prevention from double pressing or press and hold etc.
Have you put in any debug lines to see what is executing when you press the button?
I would start there and make adjustments based on what you are seeing.
I am not familiar with this gpiozero, so I can't give any insight about what it may be doing, but looking at the code and given the issue you are having, I would start with some debug lines in both functions to confirm what is happening.
Thinking about it for a minute though, could you not just change the check to 'if greenButton.is_released:'? As then you know the button has already been pressed, and the amount of time it is held in for becomes irrelevant. May also want to put in a check for if the file is already playing to stop it and start it again, or ignore and continue playing (if that is the desired behaviour).
Further suggestions:
For this section of code:
# Executed Script
while True:
if greenButton.is_pressed:
green_btn_pressed()
time.sleep(.1)
print("Game Audio Start")
p = vlc.MediaPlayer("/home/pi/Desktop/10 Second Countdown.mp3")
p.play()
You want to change this to something along these lines:
alreadyPlaying = 0
# Executed Script
while True:
if greenButton.is_pressed:
green_btn_pressed()
#Check if already playing file.
if alreadyPlaying == 1:
# Do check to see if file is still playing (google this, not sure off the top of head how to do this easiest).
# If file still playing do nothing,
#else set 'alreadyPlaying' back to '0'
break
#Check if already playing file.
if alreadyPlaying == 0:
time.sleep(.1)
print("Game Audio Start")
p = vlc.MediaPlayer("/home/pi/Desktop/10 Second Countdown.mp3")
p.play()
alreadyPlaying = 1
Hopefully you get the idea of what I am saying. Best of luck!
i think you have to write something like this in your loop:
import time, vlc
def Sound(sound):
vlc_instance = vlc.Instance()
player = vlc_instance.media_player_new()
media = vlc_instance.media_new(sound)
player.set_media(media)
player.play()
time.sleep(1.5)
duration = player.get_length() / 1000
time.sleep(duration)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have already written a complete code in python but I only run it from the console and it display the results in console. I need help in building a simple UI that has a run button that will allow the code to run and display its results within the user interface window and not on the console. How do I go about this? I am still a python beginner. The already written code is for face detection and recognition.
Example code to run external python script.
import subprocess
import PySimpleGUI as sg
layout = [
[sg.Button("RUN")],
[sg.Output(size=(80, 20))],
]
window = sg.Window("Title", layout)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == "RUN":
sp = subprocess.Popen("python myscript.py", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=None)
out, err = sp.communicate()
if out:
print(out.decode("utf-8"))
if err:
print(err.decode("utf-8"))
window.close()
I have a sweep function that returns a string of data. What I want to do is have two buttons, a start button and a stop button. When the start button is hit, the sweep function is called, and once that function is finished it is called again and again and again. Running nonstop until the stop button is hit. I'm using flask.
I don't think this is a duplicate because most of the other question regarding this want it to stop at a specific time, ie. run for 45 seconds. I need mine to run continuously until the stop button is pressed.
My thought process was to do something like
#app.route('continue_sweep', methods=["GET","POST")
def continue_sweep():
while (sweep_continue flag = true):
sweep()
and then in my stop button function just have a sort of break all? I don't know if that's a thing or not, but that was my thought process.
#app.route('stop_sweep', methods=["GET","POST"])
def stop_sweep():
break all -->> end the above sweep function here
--------------------Updated code
I can do something like this
#app.route('/start_continue_sweep', methods=["GET","POST"])
def start_continue_sweep():
if init_tester_flag == False:
initialize_tester()
stop_btn_flag = False
while (stop_btn_flag == False):
sweep_continue_fun()
return render_template('sweep_results.html', sweep_str = remove_err_str)
def sweep_continue_fun():
sweep_btn()
return render_template('sweep_results.html', sweep_str = remove_err_str)
and it gives me what I want, but does not update the sweep_results.html. Is this why I would need something like what was mentioned in your answers below? Or is there an easier fix to make it update?
That's not how web requests work. One request gets one response, then it's done. If you want something to move continuously, you will have to do that with Javascript in the browser, not in the server.