Print contents of WebView in Python (Gtk3.0) - python

I have designed a very simple Python3 app that uses a WebView to render Markdown files.
Now I'd like to add the possibility to print the rendered file (with the current stylesheet that's being applied). I'm desperately trying to find some information about how to do it. Is there any tutorial or howto out there that could explain how to do it?
I've found a few examples explaining how to print PDF files from disk, but I'd like to print things directly from the WebView. Apparently there is even a Method for that but I'm failing miserably on figuring out on how to use it...
See here
Best Regards
Merlin

OK,
I finally figured it out. It appears that only the Webkit2 library is modern enough to handle printing from a Webview. I was using WebKit1 and that was the reason it wouldn't work. So I had to change my dependencies and after that it was actually quite easy...
Lets say that you have a Webview whose name is myWebView, then your function to print it's content would look like:
def onPrintButtonClicked( self, widget ):
#Set the operation up
printOperation = WebKit2.PrintOperation.new( self.myWebView )
#Run the dialog
printOperation.run_dialog()
After the dialog, you could actually check if the user has chosen to print or to cancel by checking for WebKit2.PrintOperationResponse.CANCEL or WebKit2.PrintOperationResponse.PRINT return values.
Reagards

Related

How to type into a text field in Windows Explorer?

I am trying to upload a file to a website, and when I click on the upload button (using module WebBot) it opens Windows Explorer. Am I able to output the name of the file into the File Name field? I have the full path of the file, I just need to get the actual text into the File Name box.
I'd consider two approaches here:
Use a python library specifically for interaction with the Windows GUI. I've had good experiences with Pywinauto once, seems still pretty usable at first glance. Hook this in when you expect the explorer window to open. Code may conceptually look like this - do some test run and print all available handles from the upload dialog (just guessing here as a hint, see Pywindocs):
app = Application().connect(title_re=".*Upload file", path=r"c:\windows\explorer.exe")
dlg = app.window(title_re=".*Upload file", path=r"c:\windows\explorer.exe")
app.dlg.print_control_identifiers()
Check if you could simply do a POST or similar with the corresponding data. This is a very vague alternative as you do not provide information about what to upload and what the underlying backend/concept of the website is, but in the simplest case this may even be a more elegant option. A quick search brought up this short and simple example for this: https://stackoverflow.com/a/43942648/10192615

Using Python, how to stop the screen from updating its content?

I searched the web and SO but did not find an aswer.
Using Python, I would like to know how (if possible) can I stop the screen from updating its changes to the user.
In other words, I would like to buid a function in Python that, when called, would freeze the whole screen, preventing the user from viewing its changes. And, when called again, would set back the screen to normal. Something like the Application.ScreenUpdating Property of Excel VBA, but applied directly to the whole screen.
Something like:
FreezeScreen(On)
FreenScreen(Off)
Is it possible?
Thanks for the help!!
If by "the screen" you're talking about the terminal then I highly recommend checking out the curses library. It comes with the standard version of Python. It gives control of many different aspects of the terminal window including the functionality you described.

Print my wx.Panel after formatting it

I am working on an app using wxPython and it kind of needs to print the info shown to the user. On click of the print button, The data shown in the current panel has to be formatted a bit and then be sent to the printer queue.
Google searches give me ways to print text documents. How do I go about with this?
wxPython provides a couple of printing options. You can check out the full printing framework it provides here:
http://wxpython.org/Phoenix/docs/html/printing_framework_overview.html
http://wxpython.org/Phoenix/docs/html/Printout.html
It's fairly complex, but it is also the most flexible way to print using wxPython. They do have another option though that's called HtmlEasyPrinting:
http://wxpython.org/Phoenix/docs/html/html.HtmlEasyPrinting.html
You might want to take a look at that if you're new to wxPython. Of course, you could also use the reportlab package to generate a nicely formatted PDF of your text and then send that to the printer or just open it in the user's default PDF viewer so they can print it.

NSOpenPanel unable to select files, why?

Being very new to objective-c, MacOS, and not having programmed for years I hope this is not too obvious and thus a waste of anyone's time:
I would like to open a dialog and get the file location back as a references. From what I can tell this should be doable from within my window controller, no? Using XCode I've plumbed to the openDocument action, with an associated function like this:
class MyWindowController(NSWindowController):
#objc.IBAction
def openDocument_(self, sender):
dialog = NSOpenDialog.openPanel()
dialog.setCanChooseFile_(True)
dialog.setCanChooseDirectories_(False)
results = dialog.openModal()
# test the results and do something
The problem is that while the dialog opens right up and lets me browse around it cannot actually ever select any files. What gives?
TIA for any assistance, even if it's to tell me I missed something in the manual (just point it out please!).
You don't specify which files can be opened (using the setAllowedFileTypes_ method), or by telling the panel that all files can be selected with dialog.setAllowsOtherFileTypes_(True)

How to make PowerBuilder UI testing application?

I'm not familiar with PowerBuilder but I have a task to create Automatic UI Test Application for PB. We've decided to do it in Python with pywinauto and iaccesible libraries. The problem is that some UI elements like newly added lists record can not be accesed from it (even inspect32 can't get it).
Any ideas how to reach this elements and make them testable?
I'm experimenting with code for a tool for automating PowerBuilder-based GUIs as well. From what I can see, your best bet would be to use the PowerBuilder Native Interface (PBNI), and call PowerScript code from within your NVO.
If you like, feel free to send me an email (see my profile for my email address), I'd be interested in exchanging ideas about how to do this.
I didn't use PowerBuilder for a while but I guess that the problem that you are trying to solve is similar to the one I am trying to address for people making projects with SCADA systems like Wonderware Intouch.
The problem with such an application is that there is no API to get or set the value of a control. So a pywinauto approach can't work.
I've made a small tool to simulate the user events and to get the results from a screencapture. I am usig PIL and pytesser ORM for the analysis of the screen captures. It is not the easiest way but it works OK.
The tool is open-source and free of charge and can be downloaded from my website (Sorry in french). You just need an account but it's free as well. Just ask.
If you can read french, here is one article about testing Intouch-based applications
Sorry for the self promotion, but I was facing a similar problem with no solution so I've written my own. Anyway, that's free and open-source...
I've seen in AutomatedQa support that they a recipe recommending using msaa and setting some properties on the controls. I do not know if it works.
If you are testing DataWindows (the class is pbdwxxx, e.g. pbdw110) you will have to use a combination of clicking at specific coordinates and sending Tab keys to get to the control you want. Of course you can also send up and down arrow keys to move among rows. The easiest thing to do is to start with a normal control like an SLE and tab into the DataWindow. The problem is that the DataWindow is essentially just an image. There is no control for a given field until you move the focus there by clicking or tabbing. I've also found that the DataWindow's iAccessible interface is a bit strange. If you ask the DataWindow for the object with focus, you don't get the right answer. If you enumerate through all of the children you can find the one that has focus. If you can modify the source I also advise that you set AccessibleName for your DataWindow controls, otherwise you probably won't be able to identify the controls except by position (by DataWindow controls I mean the ones inside the DataWindow, not the DataWindow itself). If it's an MDI application, you may also find it useful to locate the MicroHelp window (class fnhelpxxx, e.g. fnhelp110, find from the main application window) to help determine your current context.
Edited to add:
Sikuli looks very promising for testing PowerBuilder. It works by recognizing objects on the screen from a saved fragment of screenshot. That is, you take a screenshot of the part of the screen you want it to find.

Categories