The root.after() loop cycles twice in one allocated time - python

I am building a tkinter GUI program which has two loops running together. People have kindly helped me with making "Start" and "Stop" buttons for it (see Creating Tkinter buttons to stop/skip a 2D loop [Solved]). However, a new problem came. In the following code, all the buttons are functioning as we expected. But the speed of the cycling is wrong.
The program is designed to print once per second. However, after finishing printing the first Row, the program starts to print twice per second. I tried a lot but cannot find the reason for that. Can anyone help. Thanks in advance!
import tkinter as tk
root = tk.Tk()
scan_array = [1,"two",3,"ten"]
running = True
Hor_index = 0
Hor_end = 7
Ver_index = 0
def Hor_scan():
global Hor_index, running, dont_run
if (Hor_index > Hor_end) and (Ver_index > len(scan_array)):
running = False
elif (Hor_index < Hor_end):
Hor_index += 1
print ("Row " + str(scan_array[Ver_index-1]) + " Column: " + str(Hor_index))
else:
running = False
Ver_scan()
if (running):
root.after (1000, Hor_scan)
def Ver_scan():
global Ver_index, Hor_index, running
if Ver_index < len(scan_array):
Hor_index = 0
Ver_index += 1
running = True
Hor_scan()
def start():
global Ver_index, Hor_index, running
Ver_index = 0
Hor_index = 0
running = True
print ("A new run started")
Ver_scan()
def skip():
global Ver_index, Hor_index
Ver_index += 1
Hor_index = 0
def stop():
global running
running = False
btnStart = tk.Button(root,text="Start",command=start)
btnSkip = tk.Button(root,text="Skip",command=skip)
btnStop = tk.Button(root,text="Stop",command=stop)
btnStart.grid()
btnSkip.grid()
btnStop.grid()
root.mainloop()

I don't see the point of having an extra if (running): statement in Hor_scan, because this
elif (Hor_index < Hor_end):
condition will anyway be only performed if it is "running". You can make the following changes
def Hor_scan():
global Hor_index, running, dont_run
if (Hor_index > Hor_end) and (Ver_index > len(scan_array)):
running = False
elif (Hor_index < Hor_end):
Hor_index += 1
print ("Row " + str(scan_array[Ver_index-1]) + " Column: " + str(Hor_index))
root.after (1000, Hor_scan)
else:
running = False
Ver_scan()
REASON : If you you call root.after from the condition as you were doing previously, you will end up scheduling one extra call per cycle, because running does not become False right after you are done with the cycle, it initiates another one, only then your program executes else which actually makes running False.

Related

How to let If-loop running all the time

i would like to know how i let my program to check the if-statement all the time while the others functions still running.
def back():
print("back")
counter = 0
while counter <= 1:
if statement == True:
print("hey")
counter += 1
back()
Here the statement has to be True before the function back can be executed, but I want it all at the same time to run
Is this what you're looking for? This will trigger back() to run in the background every time statement is True and it will store a reference to the background process in background_processes
import multiprocessing
def back():
print("back")
counter = 0
background_processes = []
while counter <= 1:
if statement == True:
print("hey")
p1 = multiprocessing.Process(target=back)
p1.start() # back() will run in the background and this loop will keep going
background_processes.append(p1)
counter += 1

Continue running a loop until a button is pressed using gpiozero - button press not registered

I have some python code on a raspberry pi that I want to run and carry on a loop until a button is pressed.
The button.wait_for_press() is not suitable because that pauses the program until it runs, but I have tried it out to see if the hardware is working and it does.
def shutterPressed():
global shutterHasBeenPressed
shutterHasBeenPressed = True
def main():
"""
Main program loop
"""
#start camera preview
camera.start_preview(resolution=(SCREEN_W, SCREEN_H))
#Display Intro screens
intro_image_1 = REAL_PATH + '/assets/intro_1.png'
intro_image_2 = REAL_PATH + '/assets/intro_2.png'
overlay_1 = overlay_image(intro_image_1, 0, 3)
overlay_2 = overlay_image(intro_image_2, 0, 4)
#Wait for button press
i = 0
blink_speed = 10
button.when_pressed = shutterPressed
while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")
When I run this code, the button press is not registered at all.
What have I got wrong?
EDIT: so I added a print statement to the shutterPressed() function and confirmed that it is running when the button is pressed.
In also added a statement to print the value of shutterHasBeenPressed just before the if statement. This never changed from false.
However, if I removed the line changing the variable to false at the beginning of the loop, then code worked, so it is obviously something to do with when various bits get run. Maybe the while loop starts again after the shutterPressed() function runs?
Either way, i have fixed it by moving the reassignment of the variable to after the if statement.
Welcome to the world of concurrency (somewhat)!
Just imagine your program running: the instructions are being executed one after the other in the order in which they are written, according to the execution flow that they define with the exception of shutterPressed which is asynchronously executed (maybe).
Therefore, imagine we enter the loop and are at the first line, <here>:
while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False # <here>
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")
Now, shutterHasBeenPressed has been set to False and the condition that follows is verified so that we enter the if.
the program keeps running until, unexpectedly, the button is pressed. Say, it reached <here>:
while True:
global shutterHasBeenPressed
shutterHasBeenPressed = False
#Stay in loop until button is pressed
if shutterHasBeenPressed is False:
i += 1
if i == blink_speed:
overlay_2.alpha = 255 # <here>
elif i == (2 * blink_speed):
overlay_2.alpha = 0
i = 0
#Restart while loop
sleep(0.1)
continue
#button has been pressed!
print("Button Pressed!")
At this point, shutterPressed runs, sets shutterHasBeenPressed to True. Then, back in our loop, the iteration finishes, we continue at the start of the loop and ... what's there?!
shutterHasBeenPressed = False
and the button press just went completely unnoticed!
I believe this answers your question asking what you have got wrong.

python - can't get audio player working

everything works except the next song doesn't play after the first is finished.
import os, random
from pygame import mixer
from pynput import keyboard
startup = 0
pause = 0
volume = 0.5
def Picker():
global startup
global volume
startup += 1
if startup > 1:
ThisSong = random.choice(os.listdir("C:\\Users\\...\\Music"))
NextSong = random.choice(os.listdir("C:\\Users\\...\\Music"))
ThisSong = NextSong
if ThisSong != NextSong:
mixer.init()
mixer.music.load("C:\\Users\\...\\Music" + ThisSong)
mixer.music.play(0)
mixer.music.set_volume(volume)
while mixer.music.get_busy():
def on_press(key):
global pause
global volume
if key == keyboard.KeyCode(char='-'):
volume -= 0.1
if volume < 0.1:
volume = 0.1
mixer.music.set_volume(volume)
if key == keyboard.KeyCode(char='='):
volume += 0.1
if volume > 1:
volume = 1
mixer.music.set_volume(volume)
if key == keyboard.KeyCode(char='['):
pause += 1
if pause == 1:
mixer.music.pause()
pause = 2
if pause == 3:
mixer.music.unpause()
pause = 0
with keyboard.Listener(on_press=on_press) as listener: listener.join()
else:
Picker()
else:
pass
Picker()
Picker()
screenshot of code
I can't get it to work, i'm very new to python so i'm probably missing something
obvious
Before starting: Thx #JGreenwell for copying the code.
Ok, so first, I’ll help you clean your code.
Things that are wrong
Having all that ThisSong and NextSong things: It won’t get saved when you restart Picker(). Either just have ThisSong, or place the ThisSong and NextSong assignment with the volume and pause variables:
.
pause = 0 # this is a problem! Next point
volume = 0.5
ThisSong = random.choice(...)
NextSong = random.choice(...)
The pause variable should be a boolean (True/False) and the pausing code should be like this:
.
pause = not pause
if pause:
# pause
else:
# unpause
Also, it would ideally be called paused
on_press and the Listener declaration should be outside the while loop, because otherwise they just keep being declared every time it loops. Then you should import time and put time.sleep(500) or something like that in the while loop, so that it doesn’t check every fraction of second.
As for the next song not playing, I don’t really know, but I’d suggest entering Picker() in the shell after the program has run (python -i script.py from CMD, IDLE leaves you in a shell by default). I would also suggest really following recommendation #3 as having them in the loop may break the loop and stop it from finishing. Most importantly, I would ask you to debug your code by adding print in every step of your code:
print(1)
if something:
print(2)
etc...
And seeing where it blocks
P.S.: The random dots are because you can’t have code in lists, so I had to exit the list.
Working solution, incase anyone else has the same problem as me in the future :)
from pygame import mixer
from pynput import keyboard
import threading
import random
import os
paused = 0
def player():
song = random.choice(os.listdir("C:\\users\\...\\desktop\\music"))
mixer.init()
mixer.music.load("C:\\users\\...\\desktop\\music\\" + song)
mixer.music.play(0)
while mixer.music.get_busy():
pass
else:
player()
def main():
t = threading.Thread(target = player, name = 'thread1', args = ())
t.start()
main()
def on_press(key):
global paused
if key == keyboard.KeyCode(char='['):
paused = not paused
if paused:
mixer.music.pause()
else:
mixer.music.unpause()
with keyboard.Listener(on_press=on_press) as listener: listener.join()

Python 2 game coordinates class

I wanted to create a x-y coordinate system even though this is supposed to be a text RPG as to keep track of where everything is. So, I was experimenting on making a function and test for that function that would let the character move on a x-y grid, however, no matter what I try, I cannot make it work. Here is the code:
class Player:
def movement(charactor_movement):
proceed = 0
if charactor_movement == "left":
character.position_x = character.position_x - 1
proceed = 1
elif charactor_movement == "right":
character.position_x = character.position_x + 1
proceed = 1
elif charactor_movement == "forward":
character.position_y = character.position_y + 1
proceed = 1
elif charactor_movement == "backward" or charactor_movement == "back":
character.position_y = character.position_y - 1
proceed = 1
charactor = Player()
charactor.position_x = 0
charactor.position_y = 0
proceed = 0
while proceed == 0:
print "You are at",
print charactor.position_x,
print"x and",
print charactor.position_y,
print"y."
global charactor_movement
charactor_movement = raw_input("Where are you going?")
charactor.movement()
At this point, it does what it is supposed to do up to changing the coordinates, as it prints "You are at 0 x and 0 y" and "Where are you going?" no matter what I type. I have tried adding an else to the function which it defaulted to no matter what I typed and gave me "Sorry, I cannot understand you." Any comments on fixing or generally improving the code would be appreciated.(Note: For the testing I purposely did not add a way to exit. The class is what i need fixed.)
You are getting the same coordinates with each iteration because your values within your while loop are not changing. Incrementing character.position_x within movement will never change the value of character.position_x within your while loop, as it is outside your function's scope. You have to use the global keyword within your movement function for each variable you are changing should you want your current logic to remain the same. Additionally, why not just pass charactor_movement as a parameter to your movement function, as opposed to using global as you currently are doing.
A minimal example:
Consider the following:
def somefunct(x):
mycode = x
mycode = 'no codez'
while True:
print mycode
codez = raw_input('gimme teh codez: ')
somefunct(codez)
which outputs
>>>[evaluate untitled-1.py]
no codez
gimme teh codez: codez!
no codez
Declaring mycode as global in the function places it in the scope of the while loop when assigned, thus
def somefunct(x):
global mycode #make variable global here
mycode = x
mycode = 'no codez'
while True:
print mycode
codez = raw_input('gimme teh codez: ')
somefunct(codez)
results in the output
>>>[evaluate untitled-1.py]
no codez
gimme teh codez: codez!
codez!

How do I have numbers increment slowly over a course of time throughout runtime

I am trying to make a text based game in which the user is a pilot in space. I want to create a movement system but am unsure how to do it. I want the user to be able to put in the desired grid coordinates, and his vehicle will begin to change its grid coords to get closer and closer to the ones he inputted.
Now, to do this I will probably need multithreading and a time element. But I am unsure how I can use a time element. Any advice is greatly appreciate, i'm just trying to learn here. Thanks guys!
from Gundam2 import Mobilesuits
#Main Variable/Object declarations:
Leo1=Mobilesuits(100,100,"Leo","leo desc","dockpit desc",100,[100,100,100])
Leo2=Mobilesuits(100,100,"Leo","leo desc","dockpit desc",100,[300,100,100])
Leo3=Mobilesuits(100,100,"Leo","leo desc","dockpit desc",100,[100,150,100])
currentmobilesuit=Leo1
#Main Function declarations
def commands(user_input,currentmobilesuit):
if user_input == "radar":
currentmobilesuit.radar()
elif user_input == "commands":
print("Command list:\nradar")
else:
print("Invalid command\nType 'commands' for a list of valid commands")
#Main execution
while True:
commands(raw_input(),currentmobilesuit)
class Mobilesuits:
#class global variables/methods here
instances = [] #grid cords here
def __init__(self,armor,speed,name,description,cockpit_description,\
radar_range, coordinates):
Mobilesuits.instances.append(self)
self.armor=armor
self.speed=speed
self.name=name
self.description=description
self.cockpit_description=cockpit_description
self.radar_range=radar_range
self.coordinates=coordinates
def can_detect(self, other):
for own_coord, other_coord in zip(self.coordinates, other.coordinates):
if abs(own_coord - other_coord) > self.radar_range:
return False
return True
def radar(self):
for other in Mobilesuits.instances:
if other is not self and self.can_detect(other):
print "%s detected at %s" % (other.description, other.coordinates)
Games typically have a "master loop" of some kind; yours does here:
#Main execution
while True:
commands(raw_input(),currentmobilesuit)
The simplest thing to do is to count in the loop:
#Main execution
turn_count = 0
while True:
commands(raw_input(),currentmobilesuit)
turn_count += 1
If you wanted the real time taken to have some impact on the counter, or be the counter, you can get the current time from the time module calling time.time().
#Main execution
import time
time_start = time.time()
time_elapsed = 0
while True:
commands(raw_input(),currentmobilesuit)
time_elapsed = time.time() - time_start
A couple other thoughts:
Make a Game class, and put the turn counter and game loop in that.
Have the commands function return a number that is the number of time units that took place during the command; for example, entering an invalid command might take 0 turns, while repairing a robot might take 5.
#Main execution
turn_count = 0
while True:
turns_taken = commands(raw_input(),currentmobilesuit)
turn_count += turns_taken
You can use non-blocking I/O. This will help you avoid the complications of threading. Here's your sample code implemented with a non-blocking read of stdin:
#!/usr/bin/python
import sys
import select
call_count = 0
#Main Function declarations
def commands(user_input):
global call_count
if len(user_input) > 0:
print('call count: ' + str(call_count) + ' user entered: ' + user_input)
def raw_input_no_block():
global call_count
call_count = call_count + 1
input_avail = select.select([sys.stdin], [], [], 0.1)[0] #wait for 0.1 seconds
if input_avail:
return sys.stdin.readline()
else:
return ''
#Main execution
while True:
commands(raw_input_no_block())

Categories

Resources