Im trying to get a return value from a function I have. I am getting the value but it is running through the function again and then getting the value even though i just want the value.
import pygame
def create_input_window():
pygame.init()
screen_width = 400
screen_height = 150
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Input Window")
input_font = pygame.font.Font(None, 32)
input_text = ""
city_input = ""
input_active = True
city_entered = False
while input_active:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return None, None
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
if not city_entered:
city_entered = True
else:
input_active = False
elif event.key == pygame.K_BACKSPACE:
if not city_entered:
input_text = input_text[:-1]
else:
city_input = city_input[:-1]
else:
if not city_entered:
input_text += event.unicode
else:
city_input += event.unicode
screen.fill((255, 255, 255)) # Clear the screen
pygame.draw.rect(screen, (0, 0, 0), (50, 10, 300, 50), 2) # Draw input box for input_text
pygame.draw.rect(screen, (0, 0, 0), (50, 70, 300, 50), 2) # Draw input box for city_input
text_surface = input_font.render(input_text, True, (0, 0, 0))
screen.blit(text_surface, (55, 15)) # Display input_text
text_surface = input_font.render(city_input, True, (0, 0, 0))
screen.blit(text_surface, (55, 75)) # Display city_input
pygame.display.update()
with open("input.txt", "w") as file:
file.write(input_text)
return str(city_input)
def get_city_input():
city_input = create_input_window()
return city_input
def main():
while True:
city_input = get_city_input()
print("City name:", city_input)
if __name__ == "__main__":
main()
I tried using a global variable instead of the function to get the variable but i dont know if i did it correctly. I asked chatgpt and got this which is similar to what I tried. I also am not too familiar with python and how it works.
import pygame
Global variable to store the city input
city_input = “”
def create_input_window():
global city_input # Access the global city_input variable
pygame.init()
screen_width = 400
screen_height = 150
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption(“Input Window”)
input_font = pygame.font.Font(None, 32)
input_text = ""
input_active = True
city_entered = False
while input_active:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return None
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
if not city_entered:
city_entered = True
else:
input_active = False
elif event.key == pygame.K_BACKSPACE:
if not city_entered:
input_text = input_text[:-1]
else:
city_input = city_input[:-1]
else:
if not city_entered:
input_text += event.unicode
else:
city_input += event.unicode
screen.fill((255, 255, 255)) # Clear the screen
pygame.draw.rect(screen, (0, 0, 0), (50, 10, 300, 50), 2) # Draw input box for input_text
pygame.draw.rect(screen, (0, 0, 0), (50, 70, 300, 50), 2) # Draw input box for city_input
text_surface = input_font.render(input_text, True, (0, 0, 0))
screen.blit(text_surface, (55, 15)) # Display input_text
text_surface = input_font.render(city_input, True, (0, 0, 0))
screen.blit(text_surface, (55, 75)) # Display city_input
pygame.display.update()
with open("input.txt", "w") as file:
file.write(input_text)
def get_city_input():
global city_input # Access the global city_input variable
return city_input
def main():
create_input_window() # Call create_input_window once to set up the input window
while True:
city_input = get_city_input()
print(“City name:”, city_input)
if name == “main“:
main()