How to fix 'NameError: sys not defined' [duplicate] - python

This question already has answers here:
python: sys is not defined
(4 answers)
Closed 3 years ago.
I have some Pygame code that is working fine up until I try to close the program. The program keeps telling me that sys is not defined. I am using the more python programming for the absolute beginner to get the hang of Pygame.
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,500))
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
My expected result is that the window will close but instead the window stays open and this error is spat out: NameError: name 'sys' is not defined

sys is a module that you need to import, preferably at the beginning.
import sys

Related

Trying to make an idle program which shutdown my computer after a length of time [duplicate]

This question already has answers here:
closing tkmessagebox after some time in python
(4 answers)
Closed last month.
Im trying to make an idle program which shuts down my computer after a length of time. At the moment, I am testing tkinter yes or no prompt to make sure if the user is still awake. I want it to be if the user hasn't answered in 30 seconds, then it moves the mouse over to No and clicks on it. But it seems I can't put both tkinter prompt and pyautogui together.
import tkinter as tk
from tkinter import messagebox
import time
import pyautogui
while True:
result = messagebox.askyesno(
title='Yes No Demo',
message='Are you awake?',
detail='Click No to stay active'
)
pyautogui.moveTo(1026, 620, duration = 0)
time.sleep(3)
pyautogui.click()
if result == "True":
time.sleep(3)
else:
break
Well, your question title and code don't make sense together. If you wonder how you can shut your PC down, import sys lib and learn about the CMD shutdown commands.

Annoying error: pygame.error: video system not initialized [duplicate]

This question already has answers here:
What is the difference between .quit and .QUIT in pygame
(2 answers)
Closed 1 year ago.
I've recently started using Pygame but I came across this error and can't seem to find a reason as to why this has happened.
My code:
import pygame
pygame.init()
screen = pygame.display.set_mode((400,500))
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.quit():
done = True
pygame.display.flip()
The error I get:
pygame.error: video system not initialized
You have made a small error write pygame.QUIT instead of pygame.quit(). This causes pygame to quit, so the error makes sense.

How do stop or end a thread and then start it again? [duplicate]

This question already has answers here:
How to start and stop a thread
(3 answers)
Closed 4 months ago.
Here's something that I've tried for a snake game, but I get an error.I did come up with another way,(that is, redefining the thread every time the snake touches a coin) but am still confused as to what the problem was, and how to solve it.
import playsound
import threading
import time
def coin():
playsound.playsound('coin.mp3')
ding = threading.Tread(target=coin)
ding.start()
time.sleep(5)
ding.start()
RuntimeError: threads can only be started once
Try this, it works for me. Source: https://stackoverflow.com/a/20021547/13300960
from pygame import mixer # pip install pygame
import threading
import time
def coin():
mixer.init()
mixer.music.load(r"coin.mp3")
mixer.music.play()
threading.Thread(target=coin).start()
time.sleep(5)
threading.Thread(target=coin).start()
time.sleep(5)

nothing happens after pygame image blit [duplicate]

This question already has answers here:
Could not open resource file, pygame error: "FileNotFoundError: No such file or directory."
(1 answer)
Is there any other way to load a resource like an image, sound, or font into Pygame? [closed]
(1 answer)
Closed 2 years ago.
I recently started to learn to use the pygame module and I've run into my first blocker. I an trying to paste an image of a circle in a grid that I created on top of my solid background. For some reason when I blit the image and update the screen nothing appears and I am not sure why as I am pretty sure that my code is correct. I've pasted my code below. Maybe you guys will see what I am missing.
from pygame import *
def main():
init()
display.set_caption("Othello")
screen = display.set_mode((480,480))
screen.fill((0,128,0))
draw.line(screen,(0,0,0),(60,0),(60,480))
draw.line(screen,(0,0,0),(120,0),(120,480))
draw.line(screen,(0,0,0),(180,0),(180,480))
draw.line(screen,(0,0,0),(240,0),(240,480))
draw.line(screen,(0,0,0),(300,0),(300,480))
draw.line(screen,(0,0,0),(360,0),(360,480))
draw.line(screen,(0,0,0),(420,0),(420,480))
draw.line(screen,(0,0,0),(0,60),(480,60))
draw.line(screen,(0,0,0),(0,120),(480,120))
draw.line(screen,(0,0,0),(0,180),(480,180))
draw.line(screen,(0,0,0),(0,240),(480,240))
draw.line(screen,(0,0,0),(0,300),(480,300))
draw.line(screen,(0,0,0),(0,360),(480,360))
draw.line(screen,(0,0,0),(0,420),(480,420))
display.flip()
image_white = image.load("White_Side.jpg")
image_black = image.load("Black_Side.jif")
screen.blit(image_white,(0,0))
screen.blit(image_black,(60,0))
display.update()
running = True
while running:
for pygame_event in event.get():
if pygame_event.type == QUIT:
running = False
if __name__=="__main__":
main()

Where's the error in this simple pygame program? (sys.exit() not working) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Pygame programs hanging on exit
Ran this simple program:
import pygame, sys
pygame.init()
screen = pygame.display.set_mode((800,600))
while True:
#process input
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
Got this simple message in IDLE:
Traceback (most recent call last):
File "C:/Users/Aerovistae/Desktop/GD_in_class", line 12, in <module>
sys.exit()
SystemExit
And the program stops responding and has to be Ctrl+Alt+Del'd. I can't see where I'm going wrong here, this is as basic as I can get. I was following what a professor did in lecture, I don't see any difference between my code and his. Can anyone suggest what might cause the problem?
Proper way to quit pygame is to call
pygame.quit()
after the main run loop.
Read From the pygame documentation: http://www.pygame.org/wiki/FrequentlyAskedQuestions#In IDLE why does the Pygame window not close correctly?
Just exit the main run loop instead of sys.exit() and end the program.

Categories

Resources