how to write an "#" with pyautogui - python-3.x

So basically This is my code:
randnum = random.randint(1, 999999)
randomname = "justalollipop" + str(randnum)
randomemail = randomname + "#gmail.com"
And after that I want to write it into a login field with pyAutoGUI with pyautogui.typewrite(randomemail)
The problem now is, that everytime it tries to write this, it just writes without the #
Edit: I know that # is an operator.
Edit//2:
So I basically looked into pyAutoGUI and found out that the code has to be
pyautogui.typewrite(randomname)
pyautogui.keyDown('altright')
pyautogui.keyDown('q')
pyautogui.keyUp('altright')
pyautogui.keyUp('q')
pyautogui.typewrite("gmail.com")
Instead of just
pyautogui.typewrite(randomemail)
Thank you for the answers

Best answer I've found after many tries:
import pyautogui
import pyperclip
pyperclip.copy("#")
pyautogui.hotkey("ctrl", "v")

As the documentation states
You can only press single-character keys with typewrite(), so you
can’t press the Shift or F1 keys, for example.
Why don't you try:
pyautogui.typewrite(randomemail)
pyautogui.press('#')
pyautogui.typewrite("gmail.com")
P.S. Maybe is just an encoding problem and it's possible to pass the # encoded. But I really don't know the inner internals of pyautogui.
I used to use pywinauto but it only supports windows as the name states.

Related

How can i create a terminal like design using tkinter

I want to create a terminal like design using tkinter. I also want to include terminal like function where once you hit enter, you would not be able to change your previous lines of sentences. Is it even possible to create such UI design using tkinter?
An example of a terminal design may look like this:
According to my research, i have found an answer and link that may help you out
Firstly i would like you to try this code, this code takes the command "ipconfig" and displays the result in a new window, you can modify this code:-
import tkinter
import os
def get_info(arg):
x = Tkinter.StringVar()
x = tfield.get("linestart", "lineend") # gives an error 'bad text index "linestart"'
print (x)
root = tkinter.Tk()
tfield = tkinter.Text(root)
tfield.pack()
for line in os.popen("ipconfig", 'r'):
tfield.insert("end", line)
tfield.bind("<Return>", get_info)
root.mainloop()
And i have found a similar question on quora take a look at this
After asking for additional help by breaking down certain parts, I was able to get a solution from j_4321 post. Link: https://stackoverflow.com/a/63830645/11355351

Write question mark in url

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')

From 'a' to Qt.Key_A in PyQt5

I'm sure it's basic, but I've searched and came back empty-handed.
I'm using Python 3.6.4 and PyQt5.
I want to store some custom action keys in a config file (via configparser), and then retrieve them and respond to that keypress event.
So basically I'm looking for a function in PyQt5 that performs the reverse of chr(Qt.Key_A) - from a character, returns a Qt.Key_.
I couldn't help myself with Googling this time, and PyQt5 is huge to peruse. I was wondering if someone could point me to the right direction.
I could use a dict, but I'm sure there must be a function that does it - I'm just not finding it.
My solution was to store the keys as ASCII code with ord(), since they can be directly compared to Qt.Key_ objects:
from PyQt5.QtCore import Qt
ord('A') == Qt.Key_A
Out[2]: True
If we are talking about alphanumeric keys only, getattr(Qt, f"Key_{key.upper()}" should work.
from PyQt5.QtCore import Qt
def string_to_key_converter(s):
attribute = f"Key_{s.upper()}"
if hasattr(Qt, attribute):
return getattr(Qt, attribute)
else:
raise ValueError(f"Key {s} is invalid or unsupported.")
> string_to_key_converter("a") is Qt.Key_A
>>> True

Preventing jedi to complete everything after space

I am trying to use jedi to complete python code inside a PyQt application, using QCompleter and QStringListModel to store the possible completion.
Here's a simple working demo:
#!/usr/bin/env python3
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import jedi
import sys
class JediEdit(QLineEdit):
def __init__(self, parent=None):
super().__init__(parent)
self._model = QStringListModel()
self._compl = QCompleter()
self._compl.setModel(self._model)
self.setCompleter(self._compl)
self.textEdited.connect(self.update_model)
def update_model(self, cur_text):
script = jedi.Script(cur_text)
compl = script.completions()
strings = list(cur_text + c.complete for c in compl)
self._model.setStringList(strings)
if __name__ == '__main__':
app = QApplication(sys.argv)
line = JediEdit()
line.show()
sys.exit(app.exec_())
If you run the application and write a code which is not completing anything (e.g. or foo =), the completion will actually show all the possible tokens that can go in that position.
So, if I run and write a space in the field, lots of things pops up, from abs to __version__.
I would like to prevent this: is it possible to query jedi.Script to understand if the token is being completed or if a completely new token is starting?
EDIT: another little question: say that I am running an interpreter which is detached from jedi current state. How can I provide local and global variables to jedi.Script so that it will take into account those, instead of its own completions?
Autocompletion
Jedi's autocompletion will always show all possible tokens in a place. That's the whole point in autocompletion.
If you don't want that behavior just scan the last few characters for whitespace and certain other characters like = or :, it would be a very simple regex command. (You could also try to look up Jedi's internals and use the way how Jedi knows about this context. However I'm not going to tell you, because it's not a public API and IMHO regex calls suffice.)
In the future something like that might be possible. (See https://github.com/davidhalter/jedi/issues/253).
Now that I think about it, there might be another way that you could experiment with this: You can try to play with Completion.name and Completion.complete. The latter only gives you what could come after the current token, while the name would be the full thing. So you can compare and if they are equal than you might not want to display anything.
Have fun playing with the API :-)
Interpreter
If you're running an interpreter, you can use jedi.Interpreter to combine code with actual Python objects. It's pretty flexible. But please note that the current Interpreter (0.8.1) is very buggy. Please use the master branch from Github (0.9.0).

accessing clipboard via win32clipboard

I am working on a project in which i have to continuouly check clipboard content. If clipboard content matches with certain specified data, then it should be deleted from clipboard.
After doing a lot of googling, I find out that it can be done easily by win32clipboard api.
I am using Python as a programming language.
Following is a code for file(CF_HDROP) format:
import win32clipboard
import win32con
def filecopy():
try:
win32clipboard.OpenClipboard()
print win32clipboard.GetClipboardData(win32con.CF_HDROP)
win32clipboard.CloseClipboard()
except TypeError:
pass
Following is a code for text format:
import win32clipboard
def textcopy():
try:
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
print data
win32clipboard.CloseClipboard()
except TypeError:
pass
I am calling above functions in a infinite loop.
Individual function works correctly. But the problem with win32clipboard is that,
after win32clipboard.OpenClipboard() command, win32clipboard lock the clipboard and only realise it after CloseClipboard() command. In between i cant copy anything in clipboard.
How can i solve this problem??
Any other suggestion are also welcome to achieve ultimate aim.
NOTE: Its not necessary to use python. You can use any other language or any other approach.
An infinite polling loop (especially one without delays) is going to be a problem since there's no way to read the contents without locking. Instead you should look into becoming a Clipboard viewer (pywin32 and msdn), that way you are notified of the clipboard contents change and then you can inspect it (get it and get out). If you google a bit on pywin32 and WM_DRAWCLIPBOARD, you'll find some python implementations.

Resources