[英]Python: How do i run a function simultaneously as other code and update the other Code real time?
我最近開始使用pygame進行編碼,以為需要實時雷達的項目創建GUI雷達。 我遇到的問題是,我想與其余代碼同時運行Ultra()函數,以使我能夠實時更新“ Distance”變量。 我想要這個Distance變量來更新屏幕上的標簽。 任何幫助都將不勝感激。 謝謝!
import pygame
import time
import sys
import RPi.GPIO as GPIO
import os
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)
GREEN = (0,155,0)
BLUE = (0,0,255)
pygame.init()
clock = pygame.time.Clock()
menuW = 400
menuH = 250
mainDisplay = pygame.display.set_mode((menuW, menuH))
pygame.display.set_caption("Radar ALPHA 1.0")
#FONTS
fontSmall = pygame.font.SysFont(None, 25)
fontBig = pygame.font.SysFont(None , 50)
#HOMEMADE FUNCTIONS
def ultra():
global Distance
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
TRIG = 7
ECHO = 11
GPIO.setup(TRIG,GPIO.OUT)
GPIO.output(TRIG,0)
GPIO.setup(ECHO,GPIO.IN)
time.sleep(1)
print "Starting mesurement..."
GPIO.output(TRIG,1)
time.sleep(0.00001)
GPIO.output(TRIG,0)
while GPIO.input(ECHO) == 0:
pass
start = time.time()
while GPIO.input(ECHO) == 1:
pass
stop = time.time()
Distance = (stop-start) * 17000
print Distance , " CM"
def message_small(msg,colour,width,height):
screen_text = fontSmall.render(msg, True, colour)
mainDisplay.blit(screen_text, [width, height])
def message_big(msg,colour,width,height):
screen_text = fontBig.render(msg, True, colour)
mainDisplay.blit(screen_text, [width, height])
def circle(display,r):
pygame.draw.circle(display, WHITE, [500, 360], r, 3)
def button(bPosX,bPosY,bX,bY):
pygame.draw.rect(mainDisplay, GREEN, [bPosX, bPosY, bX, bY] )
menuOn = True
guiOn = True
radarOn = True
dataOn = True
infoOn = True
#VARs
radarSetup = True
ultraSetup = True
targetSpeed = 5
targetX = 200
targetY = 100
changeTX = 0
changeTY = 0
#MAIN BIT
while guiOn:
while menuOn:
#VARs and SETUP
mainDisplay.fill(WHITE)
message_big("Radar Menu ALPHA 1.0", RED, 5,5)
message_small("Press R to go to Radar", BLACK, 5,50)
message_small("Press D to go to Data", BLACK , 5, 70)
message_small("Press I to go to Info" , BLACK, 5, 90)
message_small("Press Q to Quit" , BLACK, 250,220)
pygame.display.update()
#EVENT HANDLER
for event in pygame.event.get():
if event.type == pygame.QUIT:
menuOn = False
radarOn = False
dataOn = False
infoOn = False
guiOn = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
menuOn = False
dataOn = False
infoOn = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
menuOn = False
radarOn = False
infoOn = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_i:
menuOn = False
radarOn = False
dataOn = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
radarOn = False
dataOn = False
infoOn = False
guiOn = False
menuOn = False
#LOGICS
while radarOn:
while radarSetup:
pygame.init()
clock = pygame.time.Clock()
menuW = 1000
menuH = 720
radarDisplay = pygame.display.set_mode((menuW, menuH))
pygame.display.set_caption("Radar ALPHA 1.0")
radarSetup = False
#CLEAR and setup
radarDisplay.fill(WHITE)
message_big("RADAR v1.0",RED, 400 , 20)
#EVENT HANDLER
for event in pygame.event.get():
if event.type == pygame.QUIT:
menuOn = False
radarOn = False
dataOn = False
infoOn = False
guiOn = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
changeTX = - targetSpeed
if event.key == pygame.K_RIGHT:
changeTX = targetSpeed
if event.key == pygame.K_UP:
changeTY = - targetSpeed
if event.key == pygame.K_DOWN:
changeTY = targetSpeed
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
changeTX = 0
if event.key == pygame.K_RIGHT:
changeTX = 0
if event.key == pygame.K_UP:
changeTY = 0
if event.key == pygame.K_DOWN:
changeTY = 0
#Circle Set UP
pygame.draw.circle(radarDisplay, BLACK, [menuW/2, menuH/2], 300)
pygame.draw.circle(radarDisplay, GREEN, [menuW/2, menuH/2], 20)
circle(radarDisplay,50)
circle(radarDisplay,100)
circle(radarDisplay,150)
circle(radarDisplay,200)
circle(radarDisplay,250)
#LOGICS
targetX += changeTX
targetY += changeTY
pygame.draw.circle(radarDisplay, RED, [targetX, targetY], 5)
pygame.display.update()
clock.tick(2)
while dataOn:
#CLEAR
mainDisplay.fill(WHITE)
message_small("DATA",RED, 150 , 100)
pygame.display.update()
#VARs
#EVENT HANDLER
for event in pygame.event.get():
if event.type == pygame.QUIT:
menuOn = False
radarOn = False
dataOn = False
infoOn = False
guiOn = False
#LOGICS
while infoOn:
#CLEAR
mainDisplay.fill(WHITE)
message_small("INFO",RED, 150 , 100)
pygame.display.update()
#VARs
#EVENT HANDLER
for event in pygame.event.get():
if event.type == pygame.QUIT:
menuOn = False
radarOn = False
dataOn = False
infoOn = False
guiOn = False
#LOGICS
clock.tick(10)
pygame.quit()
quit()
線程與代碼分開運行。 如果要在線程之間共享數據,請確保它們是包含數據的線程安全對象,否則在更改數據后發生寫入錯誤或其他錯誤之后,您將被讀取。 https://docs.python.org/3.4/library/threading.html
另一種方法是使用用於某些主要處理的多處理庫。
import threading
import queue
import time
def mymethod(storage, stop):
"""Continuously add counters to the given storage queue.
Args:
storage (queue.Queue): Hold the data.
stop (threading.Event): Thread safe signal for when to quit.
"""
counter = 0
while not stop.is_set():
storage.put(counter)
counter += 1
time.sleep(0.1)
# end mymethod
if __name__ == "__main__":
storage = queue.Queue()
stop = threading.Event()
th = threading.Thread(target=mymethod, args=(storage, stop))
# th.daemon = True # Daemon threads are cleaned up automatically
th.start() # Run
for i in range(10):
# We don't actually know how fast each program runs. Order will not be saved.
print(storage.get(0)) # Pull the thread counter number. 0 means no wait
storage.put(i) # Set my counter number
time.sleep(0.2) # Just to show things aren't happening in any order.
time.sleep(1) # Do other stuff and forget about it.
stop.set() # Clear the stop signal, so the thread can leave the loop and quit
th.join(0) # Safely close the thread when it is done
print("thread closed")
while not storage.empty():
print(storage.get(0))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.