It has been a while since i have last visited stackoverflow, But i am happy to be back.
Now. Maybe some of you remember the 'Crazy PC "Virus"', (Program that constantly opens and closes your disk drive and maybe plays a song and shows a picture, But is harmless and should be made to be easy to close)
Well. For the next april fools i wanted to use this, but i cant find it.
So i decided that i needed to make it myself!
I have found some code to control the disk drive,
import time, pygame.cdrom as cdrom
#time.sleep(5)
while 1:
cdrom.init()
cd = cdrom.CD(0)
cd.init()
cd.eject()
cd.quit()
cdrom.quit()
time.sleep(5)
(Thank you shubham1810)
But i am not certain if it is python 3 and it is a while, meaning (For all i know, Still a noob at code) that it wont run any other code until the while ends.
But as stated before i also want a picture to be on the screen and a sound file to play...
Have you guys got any ideas? Because i dont :(
-Natan
EDIT 1:
Ok, i have now finally been able to install pygame, and this code works on python 3. What puzzles me is that it opens the drive one time and then doesnt close it again. Does anyone know how to do this?
EDIT 2:
I have decided that this wont work and have since found a working vbs script for the cd drive opening and closing. The script is:
Set oWMP = CreateObject("WMPlayer.OCX.7")
Set colCDROMs = oWMP.cdromCollection
do
if colCDROMs.Count >= 1 then
For i = 0 to colCDROMs.Count -1
colCDROMs.Item(i).Eject
Next
For i = 0 to colCDROMs.Count -1
colCDROMs.Item(i).Eject
Next
End If
wscript.sleep 5000
loop
I will call it within python and then the picture and the song, but that still doesnt solve my problem. I still need and a picture and a song to play in the background (So without the user finding them).
Any idears?
Related
This must be the most weird and confusing problem I've ever seen. So I've been trying to open an html file with python using the:
import webbrowser
webbrowser.open_new_tab()
method, at first it worked as intended but then problems started to appear. When I ran the program the file would open, but after 1 second it would close. This would always happen when I ran the program with VS code but when I ran the python program in IDLE (v3.10.0) the program ran just fine. I'm out of my depth here and I have no idea what could be causing this problem.
def visit_website():
print('Would you like to visit our website ?')
def_a=input()
if def_a=='yes' or def_a=='Yes' or def_a=='ok' or def_a=='Ok' or def_a=='OK':
webbrowser.open_new_tab('demofile.html')
Hey guys I'm new to python and I have spent roughly 30 hours trying to figure out how to fix this issue. Sorry that this is so long I tried giving as much info about what I tried already.
I am searching for a cat walking in range of my standing character in a full screen game using pyautogui locateOnScreen(). The problem is that the image is found when I have the shell or command prompt window running the script open in front of my game but if I start the script and focus the game screen immediately, the image is not found at all.
I created a new script with only the cat finding part since the rest of the main script is only keyboard input commands to run to the cat and then stand still.
import pyautogui
import time
import win32api
import keyboard # in my main script I use hotkeys and keyboard seemed to be the simplest route
def main():
time.sleep(3)
print('ok ready')
while not keyboard.is_pressed('c'):
if pyautogui.locateOnScreen('catplant.png', region=(260, 450, 300, 250), confidence=0.4) is not None:
print('cat found')
keyboard.press('o') # pushes button to start interacting with the cat when found
else:
print('cat not found')
time.sleep(0.1)
if __name__ == '__main__':
main()
note: the region is 100% correct + extra range since i can find the cat with the command window open and I used win32api.SetCursorPos and a for loop to outline the range and then tested to see when the cat should be in range. Also the confidence has to be 40-50 % as the background seems to be too similar to the cat image and the cat has different animations while walking.
The thing that infuriates me the most that sometimes when only the print('cat found') line is there , it will show that the cat was found, though still inconsistent. As soon as I add any command, mouse click or keyboard input after the cat is found, the script does not find the image at all besides with a command window open. Does not work when print is replaced with any other command either. I tried adding delays as I know the function can be slow. Didn't work.
I've checked that the active window is the game using code just to make sure and the results are still the same.
Also when I run the script and the cat is not found as soon as I alt+tab the cat is found if in range and the cat will also sometimes still be found despite the window now hiding the cat. This does however not work when i hide the cat with windows in other instances like in a picture of the cat in paint or the starting with command prompt window in foreground for example.
I might not be very experienced but I have a feeling it might be an issue with pyautogui. If I can't find a solution I'll have to resort to OpenCV which seems quite complicated and I would not prefer going into that for now. Thank you for taking your time to read and potentially try to solve this problem for me. This is my first post on this website.
Update I finally found a solution reading through an old thread about mouse inputs in full screen games. I run my locate on screen with the game in bordered mode and it works perfectly. For some reason full screen does not work
screenshot of my pycharm interface
Hey everyone. When I run this simple code (the tab 'youtubeyf.py
is to print 'hello world'), it produces the intended result but also another script (with the tab name "yfinance.py") result as well. In fact, the "yfinance.py" script doesn't need to be open in order for that result to appear too. It is almost as if it runs in the background, parallel without being open.
Goal: I want to run "print 'hello world'" without the dataframe from the other script appearing.
Problem: the dataframe is appearing without actually running or being open.
Troubleshoot attempts so far: I have "Alphabetted" and searched on StackOverflow, JetBrains on topics regarding reconfiguring the "run configurations", "parallels", and nothing yet.
Thank you for your time.
Edit 1: this does not solve my problem. for one, my issue isn't importing. in fact, everything is "running" smoothly, but that the results are two in one. It is analogous to two chefs preparing one meal each, say shrimp lo main and pizza, and then placing both meals onto one plate. I don't want that; I want to have shrimp lo main on Tuesday night, and then pizza Wednesday.
When importing yfinance, it could be possible that in that module, your code is structured such that when you import the module it runs a function or other code. In yfinance, you should delete any extraneous function that doesnt need to be run everytime you import yfinance somewhere else.
Edit: what you can do, if you dont want to change much of the structure yfinance.py, is wrap all the code that runs in that python file inside a main function and add the following:
if __name__=="__main__":
main()
This makes it so that python only runs main if you're actually running yfinance.py , and doesnt run main when you're importing it.
This answer on Stack Overflow offers a solution I am trying to implement. In particular, see the sections "Installation Instructions" and "How to Use".
Can anyone tell me the steps required to "restart" IDLE?
New Information:
Just as people have suggested I thought this simply meant closing the program and opening it back up again...but I already tried that.
The other twist to my situation is that I'm working on a virtual machine so I was unable to do the installation of IDLE2HTML.py myself. My work's Help Desk had to do it so I cannot speak for the accuracy of their work. For now I'm assuming they did it correctly, but when I go to the "Options" menu there is no option to "Save as HTML".
My only guess at this point is that I still need to "restart IDLE".
Just wanted to double check if there was something else I could do before going back to my Help Desk department.
IDLE reads the idlelib/config-xyz.def files, including config-extensions.def, just once, when it starts. So any changes to config-extensions.def only takes effect the next time you start IDLE.
If you do not see 'Save as HTML' after starting IDLE, the extension is not installed properly.
It means you need to close the IDLE so that any changes made by the script can affect the IDLE
Simply close the IDLE either via the X, Ctrl+Q or File>Exit, then open the IDLE again.
If you are using idlelib module from a Python program then close your program and run it again.
While working on a simple little timer I was creating to simultaneously help me learn more about Python and to help me keep on schedule with my schoolwork; I decided to add a little tune to let me know when your time was up. However, after using the following snippet of code to program each individual note:
winsound.Beep(Freq, Dur)
I realized that whenever I actually played the little song, an odd clicking sound happened between each note. Does anyone know what's going on? Should I use a different expression? Am I just losing my mind?
Edit: Since posting this, I realized what the issue is, the program is waiting about a sixteenth of second in between playing each note. So I'd like to ask another question (any answers relating to the others would still be appreciated), namely, how can I make it not wait in between the notes.
import winsound
winsound.Beep(262,500)
winsound.Beep(277,500)
winsound.Beep(294,500)
winsound.Beep(311,500)
winsound.Beep(330,500)
winsound.Beep(349,500)
winsound.Beep(370,500)
winsound.Beep(392,500)
winsound.Beep(415,500)
winsound.Beep(440,500)
winsound.Beep(466,500)
winsound.Beep(493,500)
winsound.Beep(523,500)
All the above code does is play a simple chromatic scale from middle c to c2, but once one runs it, one would immediately see what Im talking about