Python Keyboard module executing keys to next line in CLI - python

I am creating a simple number only user input for CLI and also created it. But while using keyboard module when the user tries to press enter, the program closes perfectly, but the text that user enters gets executed in command line.
import keyboard
specials = "1234567890"
userinput = ""
no_of_presses = 0
print("Number to be doubled : ",end="",flush=True)
def show(e):
global no_of_presses, userinput
if(e.name in specials):
print(e.name,end="",flush=True)
no_of_presses+=1
userinput+=e.name
def output(e):
global userinput
print(f"\noutput : {int(userinput)*2}")
keyboard.on_press(show)
keyboard.on_press_key("enter",output)
keyboard.wait("enter")
Here is the image of sample error I am getting

Related

I'm trying to write a simple whatsapp bot with selenium but the messaging loop won't work properly. What is the problem?

textbar = driver.find_element("xpath",'//*[#id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[1]/p')
while(True):
message = input("Please enter the text you want to send to the selected person to stop the program type -exit- : ")
textbar.send_keys(message)
if(message == "exit"):
break
textbar.send_keys(Keys.RETURN)
In your code, you're calling textbar.send_keys(message) before checking if the message is exit. You should move up your if statement so that you exit the program sooner. And put the find_element inside the loop to avoid StaleElementReferenceException. Eg:
while(True):
message = input("Please enter the text you want to send to the selected person. To stop the program, type -exit- : ")
if(message == "exit"):
break
textbar = driver.find_element("xpath",'//*[#id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[1]/p')
textbar.send_keys(message)
textbar.send_keys(Keys.RETURN)

While Loop No Longer Updating Data when added "Press Enter to Exit"

I am a beginner to Python and recently was making a **Discord Rich Prescense** application. The problem is that I was using a While Loop and added a "*Press Enter to Exit*" feature. This made the Rich Prescense stuck on One Quote. I have attached a screenshot of the problem.
from config import credentials
from data import quotes
from pypresence import Presence
import random
import time
def quotegen():
RPC = Presence(credentials.clientid)
RPC.connect()
while True:
RPC.update(details="Random Quote:", state=random.choice(quotes.quotes))
i = input("Press Enter to Exit")
time.sleep(30)
if not i:
break
Screenshot of what its supposed to do:
Using the keyboard module (https://pypi.org/project/keyboard/)
you can do it all.
I modified your code to fit your requirements:
import keyboard # using module keyboard
from config import credentials
from data import quotes
from pypresence import Presence
import random
import time
def quotegen():
RPC = Presence(credentials.clientid)
RPC.connect()
while True:
RPC.update(details="Random Quote:", state=random.choice(quotes.quotes))
i = input("Press Enter to Exit")
time.sleep(30)
if keyboard.is_pressed('enter'): # if key 'enter' is pressed
break

Make text appear immediately on key press

I'm making a text based game and I want the user to be able to press enter while the text is appearing letter by letter to make the remaining of the text appear immediately.
This is my code so far
import time
import sys
def print(s):
for c in s:
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(0.01)
def sceneOne():
print ("insert\n"
"text\n"
"here\n")
input("\n[Press enter to continue]\n")
print ("insert\n"
"text\n"
"here\n")
input("\n[Press enter to continue]\n")
sceneOne()
I want the "Press enter to continue" text to be under the "insert text here" while the text is appearing letter by letter so that the user can make the text appear faster if they have already played the game and want to speed run through this part to get to the next choice faster.
install the keyboard module and then you can create an event listener. Then, modify the print function so that it does not sleep if they hit enter. See below:
def print(s):
for c in s:
if keyboard.is_pressed('enter'):
sys.stdout.write(c)
sys.stdout.flush()
else:
sys.stdout.write(c)
sys.stdout.flush()
time.sleep(0.05)

Waiting for Input without Interrupting with Loops

First of all, I use windows so i couldn't make curses work in windows.
I'm trying to make a login interface while an ASCII Animation is Looping.
def animation():
while True:
#something that do animation
def login_screen():
uname = str(input("Username : "))
passw = str(input("Password : "))
print("Welcome {}".format(uname))
so how do i do this in one console screen ? this is for example :

Python File Upload If statement and user cancelation

I have the following file upload code. I want to modify this code for two things:
1.If the user cancels the askopenfilenames command, the overall entire program will stop.
2.If the user does not select the required number of files, the function fileupload will restart, until the correct number of files is selected.
import Tkinter
from Tkinter import Tk
tk=Tk()
from tkFileDialog import askopenfilenames
import tkMessageBox
def fileupload():
tk.withdraw()
uploadedfiles = askopenfilenames(multiple=True)
##if user cancels: (IF STATEMENT TO RESULT IN MESSAGEBOX AND CODE TO STOP)
###tk.withdraw()
###tkMessageBox.showinfo(message="File Upload has been cancelled program will stop")
##Stop code
if len(uploadedfiles)>2 or len(uploadedfiles)<2:
tk.withdraw()
tkMessageBox.showinfo(message="2 files have not been selected!")
##rerun function to reselect files
return uploadedfiles
uploadedfiles=fileupload()
print uploadedfiles
First check if the string returned by askopenfilenames is empty, which means that the user closed the dialog. Then use tk.splitlist to create a list from the string (if it is not empty) and check its length:
tk=Tk()
tk.withdraw()
def fileupload():
while True:
uploadedfilenames = askopenfilenames(multiple=True)
if uploadedfilenames == '':
tkMessageBox.showinfo(message="File Upload has been cancelled program will stop")
return
uploadedfiles = tk.splitlist(uploadedfilenames)
if len(uploadedfiles)!=2:
tkMessageBox.showinfo(message="2 files have not been selected!")
else:
return uploadedfiles
print fileupload()

Categories