Using Tkinter to open a webpage - python

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.

Related

How can I click buttons in a windows GUI program using python?

I am writing a python script that automates running a program and performing different tasks within the program. My main problem is figuring out how to click buttons and interact with the GUI of the program to be controlled.
I am currently using the pyautogui library and using pyautogui.click(X,Y) to advance through prompts and click on different menus and menu items. The problem with this approach is that I am relying on a separate script to inform me of the coordinates of interest in my environment by telling me the coordinates of where my cursor is hovering. This probably will not work on other machines and just seems like a one case solution.
My question is how can I automate using a program in windows (clicking around) without having to hard code the exact position of the items I need to click?
For example, If I need to click a "ok" box to accept some setting, how can I make Windows grab the program window, read through the options and click what I need without any prior knowledge of the position of the dialog box and where the "Ok" button is located?
Code:
import pyautogui as gui
gui.click(x,y)
The way you can do this using pyautogui is with their locating methods. You will need a picture (for example of the OK box) and then you can have pyautogui find it on the screen and give you its coordinates. Check out the official documentation on this.

how to create a pop up massage in python?

firstly what do you call this popup and how do you create it in python?
I don't know what do you call it, that's why I couldn't create it.
You can use the tkinter library in python.
Please see this solution How to create a modal dialog in tkinter?
This can be done with the win10toast module in python, more information can be found here on the PyPi website https://pypi.org/project/win10toast/
Note: This will not create exactly what is referenced in the picture above, but instead it creates a notification at the bottom right of a windows 10 machine.

How to create a file browser in wxpython

I am developing the GUI for my application using wxpython and have most of the features down, except in the main frame/window I want to have a box for choosing a file (in this case, the input will have to be an excel file). Something similar to the standard filebrowser that is accessed whenever you choose "open" from a menu.
Below is an image to show exactly what I want...
You probably want a wx.FileDialog. It provides access to the default file dialog of the OS your app is running in. You can see an example of how it's used in the wxPython demo package. This tutorial also has some screenshots and sample code:
http://www.blog.pythonlibrary.org/2010/06/26/the-dialogs-of-wxpython-part-1-of-2/
The screenshot you show appears to be an interface to actually open the dialog. You can easily create that using sizers and basic widgets. Then just bind the open button to a handler that will show the dialog.
You might also want to take a look at the FileBrowseButton from wx.lib.filebrowsebutton (also in the demo).
There are a few other related widgets which you might be interested in too: wx.DirDialog, MultiDirDialog or wx.GenericDirDialog.
Assuming you know the basics of wxPython you can use wx.GenericDirCtrl and wx.ListCtrl to make nice browser

pyqt embed in photoshop

This is complex to explain, I hope this will not end up being a vague question getting vague answers.
If this is not the right place to ask this, you may help me to find the proper one.
I have a plugin for Photoshop based on the Listener, so it captures any input from the user.
The plugin creates a python module (called here "ps") containing basically the hInstance and the hwnd of the photoshop window.
Then this plugin, using plain python commands in the plugin for the module like those
PyRun_SimpleString("import Photoshop");
PyRun_SimpleString("Photoshop.showTools()");
will load a special module (here called "Photoshop") that will initialize pyqt and using the QtWinMigrate and the ps module to get the hInstance like this: QMfcApp.pluginInstance(ps.GetPluginInstance()), will start pyqt in photoshop. Here an example code of the Photoshop module using the ps module:
from PyQt4.QtWinMigrate import QMfcApp
from PyQt4.QtGui import QPushButton
import ps #this is implemented in the photoshop plugin (based on the Listener plugin)
#create the plugin instance here
app=QMfcApp.pluginInstance(ps.GetPluginInstance())
def showTools():
box = QPushButton()
box.show()
app.exec_()
Again then, the sequence is like this:
When the plugin starts in photoshop "ps" module is created, then it will load the "Photoshop" module that will load and bind properly pyqt. In the "Photoshop" module I can load any python module, widgets are properly working and everything works really well inside Photoshop.
But now the problem is: using Wacom tablets in Photoshop loose stroke sensitivity, the driver works and everything else works but the pressure sensitivity.
Apparently QMfcApp.pluginInstance will install an event filter to drive the Qt event loop while photoshop still owns the event loop. ( http://doc.qt.digia.com/solutions/4/qtwinmigrate/qmfcapp.html )
and on the paper looks fine to me.. but I could not manage to solve this by myself and I tried, more or less carefully, different approaches:
the listener plugin is not the problem. If Listener plugin runs but python is not initialized sensitivity works fine.
python itself is not a problem. If the listener starts python without gui nor pyqt, then works fine.
as soon as I call pluginInstance which should create the QApplication the issue starts and pressure is lost from the tablet. Even with the small code I wrote before.
Someone may have put pyqt as a plugin somewhere else, since the only purpose of QMfcApp is apparently this one. There is something I can configure to make it work? Is a known issue?
I would rather keep the approach (instead of connecting to photoshop externally like with COM)
I am not able to post the entire code here but let me know if you need something.. I probably can show more.
Thanks a lot for your help

Python Desktop Integration - Drag and drop

I have a pygame window that I want to know when a file has been dragged and dropped onto it. I only need to be able to fetch the name of the file. How can this be accomplished?
Here's a forum thread that might be what you're looking for.
And another forum.
And a link to the msdn page. You'll probably want the pythoncom library.
one option for a similar effect is is to use pygame's scrap module so you can copy-paste into the window, your program would just need to look for ctr-V events.
On this XFCE desktop I'm using
If I hit ctrl-C with a file selected, the file name shows up when I type
pygame.scrap.init()
types= pygame.scrap.get_types()
print dict(
[type,pygame.scrap.get(type)]
for type intypes
)

Categories