I have 0 experience in coding but I put my ambition and code something in python for a game. Everything works perfect :D
But I have a problem. I don't know how to infinite repeat the code. I've look over the internet but i didn't understand much.
Im gonna let here a part of the code maybe someone can explain me how to put all code into 'repeat'.
import time
import pyautogui
pyautogui.click(942, 642)
time.sleep(1)
pyautogui.click(807, 581)
time.sleep(1)
Thanks.
Simply wrap it all (or, well, not the imports) in a while True: loop.
import time
import pyautogui
while True:
pyautogui.click(942, 642)
time.sleep(1)
pyautogui.click(807, 581)
time.sleep(1)
Related
Coming from Arduino to python I am use to everything running in a loop more or less.
I am trying to understand how python interacts with kivy.
I understand that in order to make a segment of code run over and over I need a while statement for example. However if I use code that loops before it gets to the kivy code it will never get to the kivy code. But if I make a loop after the kivy code it will not run till I close the program.
I have google around and I see examples of simple projects of python/kivy projects that all the code pertains to the UI glue logic to make it actually do something. But I have not seen anything show python code running independent of the kivy project.
In other words if I made a project in Arduino I would have a main loop and I could call out to functions and then return from them. However I don't understand what is the best way to do this with kivy/python.
The sample code I have posted below is not a loop however I would expect it to run everything in one go. But It will run the first print statements and then when I close the app the last print statement will run.
I understand that loops are not recommended with object oriented programing, this is just a simple example as reference of what I'm use to.
For those that will say I don't understand what your asking and what are you trying to do or ask?
I am trying to ask where do I put python code that dose not pertain immediately to the kivy code but needs to run in loops or whatever while kivy is running. So that I can make things happen on the python side while not blocking kivy.
Dose this require multiple python programs? And leave the kivy program by itself almost like a .kv file.
Or dose it require everything to be put in classes?
Thanks for any clarification, best practices or examples.
from kivy.app import App
from kivy.uix.button import Button
print("test")
class FirstKivy(App):
def build(self):
return Button(text="Test text")
print("test2")
FirstKivy().run()
print("test3")
You would need to add Threading to your code
from kivy.app import App
from kivy.uix.button import Button
import threading
print("test")
class FirstKivy(App):
def build(self):
return Button(text="Test text")
print("test2")
def run():
FirstKivy().run()
def print_stuff():
print("test3")
kivy_thread = threading.Thread(target=run)
print_thread = threading.Thread(target=print_stuff)
kivy_thread.start()
print_thread.start()
I was working on a larger piece of code and I kept getting an error when the program just seemed to end without doing anything. I narrowed down the problem and reproduced it below. As far as I understand the code should print the sentence and it seems to work on other online IDE's while failing on mine. Feel like I'm missing something super simple.
Failing On: IDLE Python 3.8.3 32-bit from python.org
Works On: onlinegdb.com
Code:
import multiprocessing
def x():
print("This is x func")
if __name__ == '__main__':
multiprocessing.freeze_support()
p = multiprocessing.Process(target=x)
p.start()
p.join()
Output:
>>>
I think the issue is IDLE just doesn't output stuff from stuff outside the main process. Need to use consoles which would output everything from the main and all other processes. Reference : Python multiprocessing example not working
I'm using pyautogui to perform some routines.
I'm trying to write a url that contains '?' but this character is not written in the url, how can I do this?
below my code
import pyautogui as m
#from requests.utils import requote_uri
st = '?'
url = 'http://10.100.0.34/Relatorios/Pages/Report.aspx?
ItemPath=%2fDBM%2fGrafico+Espa%c3%a7o+Servidores'
print(url)
def checklist():
m.moveTo(27,882,duration=1)
m.click(27,882)
m.moveTo(115,269,duration=1)
m.click(115,269)
m.moveTo(128,37,duration=1)
m.click(128,37)
m.typewrite(url,interval=0.02)
m.press('enter')
checklist()
OUTPUT
http://10.100.0.34/Relatorios/Pages/Report.aspxItemPath=%2fDBM%2fGrafico+Espa%c3%a7o+Servidores
I've been trying to recreate the problem for so long but I can't seem to get what's causing the problem cause everything works fine on my pc. try raising the interval or splitting the code into sections like this
typewrite('http://10.100.0.34/Relatorios/Pages/Report.aspx'+'?'+'ItemPath=%2f
DBM%2fGrafico+Espa%c3%a7o+Servidores')
also try adding a time.sleep() before the typewrite function maybe the m.click() is causing the problem.
One way to solve the ? mark problem with pyautogui is using pyperclip library.
import pyautogui
import pyperclip
pyperclip.copy('some link that has ? in it')
pyautogui.hotkey('ctrl', 'v')
pyautogui.press('enter')
I am trying to make a timer in my text-based adventure games so that for every second that passes, 1 hp is taken away from the user, while they are in a certain scenario and can still use other functions. Here's my code:
#Imports and base variables
import time
import threading
import random
hp=100 #Health
p="snow" #Starting point
invtry=[] #Inventory of Character
#Function to drop health
def hlthdrp():
hp-1
t=threading.Timer(1.0,hlthdrp)
t.start()
while p=="snow" or p=="Snow":
if hp==0 and p=="snow" or p=="Snow":
print ("You died of frostbite")
t.cancel()
threading.wait(timeout=5.0)
As of right now, I'm not getting any errors, but instead, a completely blank shell, or terminal. If anyone could help, that'd be great!
You must call threading.Timer recursively else it only calling one time
And you can use lower() function to compare snow or some string
There is your fixed code i hope it will be usefull for you.
def hlthdrp():
global hp
print(hp)# just for testing
hp=hp-1
if hp!=0:
threading.Timer(1, hlthdrp).start()
hlthdrp()
while p.lower()=="snow":
if hp==0 :
print ("You died of frostbite")
elif hp!=0 :
pass
I'm new to Python3 and I tried to use pygame library. Firstly I tried to play .mp3 file using pygame.mixer with following code:
import pygame
from pygame import mixer
mixer.init()
mixer.music.load('some.mp3')
mixer.music.play()
I tried to run this code but it just went through it and didn't do anything but showed pygame welcome message. Not even a single error. Then I tried to run this code by writing every single line in console and somehow it worked.
Similar thing happens with this code:
from pygame import *
init()
while going:
for e in event.get():
if e.type == QUIT:
going = False
if e.type == KEYDOWN:
if e.key == K_ESCAPE:
going = False
print("work")
I copied it from official pygame eventlist example and while the example runs as it's intended to it doesn't work when I use only part showed above.
What am I doing wrong here?
You haven't told your program to write anything so it hasn't.
You need to to add print statements if you want your Python program to write something to your console.
Here's an example:
print("Hello World")
You should obviously place the exact response you are looking to get inside the print statement.