Using python to input text to a different window - python

I wanted to make a python 3 program that types something in another window, such as Google Chrome.
Is this possible, and if so, how?

This question is a little open ended, but it sounds like you want an http server. You can use either SimpleHTTPServer or make your own from scratch. A basic HelloWorld for using python3's http module is here. I used this simple guide to help me understand the basics of web servers in python. Hope this helps!

import pyautogui
searchbar = (x, y)
text_or_url = "hello word!"
pyautogui.click(searchbar)
pyautogui.typewrite(text_or_url)
pyautogui.typewrite([Enter])

Use pywinauto lib
It is very easy.
For example you have opened notepad window and opened 'Save as' dialog and want to type filename:
from pywinauto import application
app = application.Application()
app.connect(title='test1.txt - Notepad')
app.SaveAs.Edit1.type_keys("C:\\test2.txt")

Use pywinauto lib
filePath.set_text('D:\\aa.txt')
inpuMsg.type_keys('D:\\aa.txt')
set_text
type_keys

Related

Is it possible to open links in a Python (3.10) terminal?

If I had a print() statement, for example:
print('https://www.google.com')
Is there a way to make a link clickable? And would the same apply for a file path, like this:
print('path/to/file')
What I need is a way to print a link/path to a terminal and then to be able to open it by clicking on it. Is this possible? If yes, how?
Python itself has nothing to do with this behavior- its functionality provided by your terminal emulator.
However, you can open a link programmatically in the browser within python using the builtin webbrowser module, for instance:
from webbrowser import open_new_tab
open_new_tab("https://www.google.com")
In some scenarios, this may be sufficient.

How to add systemtray to python script?

I want to add a systemtray to an existing python based project: https://github.com/piejanssens/premiumizer
How exactly do I need to do that? I am completely new to python and I am using it for the first time, just because I want to add a little feature to and existing script.
What I want to achieve is that when the script is running that there should be a system tray icon which opens http://localhost:5000 if it is double clicked. And if it is right clicked there should be an Exit/Quit option.
I have researched a bit and I think I could achieve it with one of these two scripts https://github.com/moses-palmer/pystray or with https://github.com/Infinidat/infi.systray (I also read that infi.systray should be used because it is not dependent on pywin32 because it uses the ctypes library because that one is part of the standard Python library).
So I tried to add this code for testing to the premiumizer.py file:
from infi.systray import SysTrayIcon
def say_hello(systray):
print "Hello, World!"
menu_options = (("Say Hello", None, say_hello),)
systray = SysTrayIcon("icon.ico", "Example tray icon", menu_options)
systray.start()
But now the Console is closing itself immediately. How can I check what went wrong? Is an error log saved somewhere ?
What do I need to do to make it work? Or is there an easier way for someone "stupid" like me ?
Welcome to the world of python!
Let me assume that you copied the script you've posted into a python file and just ran the file, correct? If so, the problem is that once the script is executed the program exits and with it the tray icon.
Start an interactive console by running python or (ipython if you have it installed) in a command window and paste in your code. You'll see that the tray icon appears and stays. It disappears once you close the console.
(Remark: the code above uses the python 2.x version of print without the () and will cause an error in python 3.x, there use print("Hello, World!").)
To make this work you need to put this code somewhere in the setup/initialization part of the premiumizer. Without knowing this project I cannot be of further help where to exactly.

Opening File Directory by using a button

Hi i am trying to do a simple program for my school project, this is the first time i am using python as i am used to c# programming
I am using Window7, Is there ways in which I can press a button in a TK GUI and this directory be pop out ?
InitDir = "C:\AgmPlots"
As other stated, you need some (platform specific) program to "open" a directory.
The most platform-independent way could possibly be the webbrowser module. (Although it has web in its name, it opens my standard file manager with file:// URLs).
import webbrowser
And somewhere inside your event handler:
webbrowser.open ('file:///home/nobody') #your path here

Using Tkinter to open a webpage

So my App needs to be able to open a single webpage(and it must be from the internet and not saved) in it, and specifically I'd like to use the Tkinter GUI toolkit since it's the one i'm most comfortable with. On top of that though, I'd like to be able to generate events in the window(say a mouse click) but without actually using the mouse. What's a good method to go about this?
EDIT: I suppose to clarify this a bit, I need a way to load a webpage, or maybe even a specific java applet into a tkinter widget or window. Or if not that perhaps another method to do this where I can generate mouse and keyboard events without using either the mouse of the keyboard.
If you want it to be opened inside your GUI use Bryans suggestion, if you just want to open a webpage you can use:
import webbrowser
webbrowser.open("page.html")
Tkinter does not have a widget that can render a web page.
So i found this module named pywebview
pip install pywebview
sample code:-
import webview
webview.create_window('duckduckgo', 'https://www.duckduckgo.com')
webview.start() #this will open the webpage in a new window
You should use pywebview it is very easy only code three lines .
I used it but in my case it didn't work everywhere. Comment and let me know if it works for you.
The best option that works everywhere is PyQt's QtWebview module. You might run into one problem that is to rename the window, so here is the solution
web.setWindowTitle(title)
You can use all the functions as it is but just replace window or self with web like the above code.

Automating Flash Program with Python using Pywinauto

I am trying to send a couple basic text commands to a flash program running in Firefox on Windows 7, but I am unable to get pywinauto working for me.
Right now, I have just been able to accomplish the very basic task of connecting to Firefox plugin-container by directing it to the path using the following code:
from pywinauto import application
app = application.Application()
app.connect_(path = r"c:\Program Files (x86)\Mozilla Firefox\plugin-container.exe")
The next step seems to be something to the effect of:
app.plugin-container.Edit.TypeKeys('Text')
However, I can't reference the plugin-container window using '.plugin-container', or any combination of those words. I have tried adding a title variable to the connect_() function and I have tried everything I can think of to find out how to type the command.
The example I am basing this off of is the notepad sample:
from pywinauto import application
app.start_(ur"notepad.exe")
app.Notepad.Edit.TypeKeys(u"{END}{ENTER}SendText d\xf6\xe9s "
u"s\xfcpp\xf4rt \xe0cce\xf1ted characters!!!", with_spaces = True)
It doesn't matter to me if I use pywinauto or Firefox. If it is any easier to do this using a different module or Internet Explorer, I'm on board for whatever accomplishes the task. I am using Python version 2.7.2 and would prefer it over any version changes.
Any help at all is appreciated. I am pretty lost in all this.
As the author of pywinauto - I think you are going to have a hard time. pywinauto only really helps with standard windows controls, and I don't think that flash controls are implemented as standard windows controls (Buttons, Edit boxes, etc).
OFf the top of my head - I would think Sikuli may be a better starting point (http://sikuli.org/).
Another option may be 'http://code.google.com/p/flash-selenium/' - I just googled for "automating flash input" - and it turned up in one of the first articles I clicked.
Thanks for trying pywinauto - I just don't think it is best suited for Flash automation.

Categories