Python exec(script, globals()) not showing GUI (iPyWidgets) - python

Hello my fellow programmers,
I want to include a script from another file that creates a UI with iPyWidgets.
The problem is that the code will execute but nothing is shown. I am using Jupyterhub to display the button.
When I run the code on itself, the button is showing.
button_code.py
from ipywidgets import widgets
from IPython.display import display
from IPython.display import clear_output
from ipywidgets import Button, HBox, VBox, Layout, Button, Text, Textarea
widgets.Button(description = 'clear',
layout=Layout(width='20%', height='100%'))
call_button_script.py
import os
def call_script():
script_dir = os.path.dirname(os.path.abspath(__file__))
script_fqn = os.path.join(script_dir, 'button_code.py')
script = open(script_fqn).read()
exec(script, globals())
The code is executed via the following import:
from call_button_script import call_script
call_script()
Am I missing something crucial or is there another possible mistake?
Thank you for your time.

Is your from call_button_script import call_script call_script() in the same directory ? did you've seen the output on the console

The answer was to just import the whole file.
I´ve put it in a class and initialized it all in the init
The GUI wasn´t showing because it was missing display() in the code

Related

how to import pre writen HTML code into TKinter

i have done research on running HTML code in python, i have the libraries imported just i have my code in a HTML file. So is there any way to import it into python?
Thanks Mrcottonball.
I have tried this code ↓↓↓
The problem is i need to use HTML outside of python and occasionally import it as a .html file
`
# Import Module
from tkinter import *
from tkhtmlview import HTMLLabel
# Create Object
root = Tk()
# Set Geometry
root.geometry("400x400")
# Add label
my_label = HTMLLabel(root, html="""
<h1>HI</h1>
<h2>HI</h2>
<h3>HI</h3>
<h4>HI</h4>
<h5>HI</h5>
<h6>HI</h6>
""")
# Adjust label
my_label.pack(pady=20, padx=20)
# Execute Tkinter
root.mainloop()
`
any help would be appreciated
kind regards, Mrcottonball :)

Python FileDialog to Get Folderpath not file

I am using Python 3.6. I also use PyQt. I use Qt Designer to develop a form. But I need a code to open a filedialog box, allow the user to navigate to the desired FOLDER (not file) and then click and print/get the folderpath. I cannot find how to do this. I appreciate anyone's help here. Thanks.
You can use the getExistingDirectory() method of QFileDialog by applying the necessary filters:
import sys
from PyQt5 import QtWidgets, QtCore
app = QtWidgets.QApplication(sys.argv)
directory = QtWidgets.QFileDialog.getExistingDirectory(None,
"Select Directory",
QtCore.QDir.currentPath())
print(directory)

QInputDialog popup without application in Pyside

How can I make a popup QInputDialog without a parent Application using Pyside?
When the user double clicks this python file I want it to display the Input dialog and that's it.
import sys
import os
from PySide import QtGui, QtCore
def network_copy():
text, ok = QtGui.QInputDialog.getText(None, 'Test | Network Copy', 'Enter name')
if ok and text:
print 'Great'
network_copy()
If you run your example, it will print a debug message like this: QWidget: Must construct a QApplication before a QPaintDevice. So, obviously, this is just not possible.
I'm guessing that all you want to do is avoid having to start an event-loop. However, this is not an issue with dialogs, because they run their own event-loops. So just create the QApplication before showing the dialog:
app = QtGui.QApplication(sys.argv)
network_copy()

Qt: Take over system open file dialog

I would like to automate the upload of a file to a website using PyQt4's QWebView, but there's a part I can't figure out yet. To upload the file, the website has a button, which opens a dialog from which you are supposed to select the local file. So, these are my questions :) Is there a way to control that dialog once I click the button? Is there a better way to achieve this?
edit
The website is https://maps.google.com/ and I'm uploading a .kml file through My Places > Create Map > Import.
It is possible what you're looking for is QWebPage::chooseFile() (I suppose it depends also on how the website is handling that). Reimplement that and see if it is sufficient. Do whatever you want and return the chosen file path.
EDIT: Now that you provided the link I tested and seems to work.
Ok, to begin let me start with background information and references.
The module that I will be using is pywin32 download here, specifically the win32gui, API reference here.
Now before you can manipulate the dialog you have to "navigate" to the window handle, the following uses win32.FindWindow API Reference here, which looks like this, where the two inputs are the lpclassName in this case #32770 (stands for a dialog) reference here and the lpWindowName which in this case is File Upload,
HWND WINAPI FindWindow(
_In_opt_ LPCTSTR lpClassName,
_In_opt_ LPCTSTR lpWindowName
);
Code to locate file handle:
import win32gui
control = win32gui.FindWindow("#32770", "File Upload")
And it stores the handle, which in my case was 721470.
The next step is locate the handles of the GUI objects in the dialog, i will show an example of the Cancel button. To find the handle, I wil be using FindWindowEx API reference here,
import win32con
import win32api
ButtonHandle = win32gui.FindWindowEx(control, 0, "Button", "Cancel");
win32api.SendMessage(ButtonHandle, win32con.BM_CLICK, 0, 0)
Reference here for the BM_CLICK and here for the SendMessage.
Final code:
import win32gui
import win32api
import win32con
window = win32gui.GetForegroundWindow()
title = win32gui.GetWindowText(window)
control = win32gui.FindWindow("#32770", "File Upload")
ButtonHandle = win32gui.FindWindowEx(control, 0, "Button", "Cancel")
win32api.SendMessage(ButtonHandle, win32con.BM_CLICK, 0, 0)
Another way is to use the watsup.winGuiAuto module, here, example below:
from watsup.winGuiAuto import *
optDialog = findTopWindow(wantedText="File Upload")
CancelButton = findControl(optDialog,wantedClass="Button", wantedText="Cancel")
clickButton(SaveButton)
But i believe the easiest way is to use autoit here, i have used it before in pyqt, to shoot out commands.
Hope this helps!
Additional References (pywin32 versions):
win32gui here
win32api here
Here's a pure PyQt4 demo that more or less reproduces the default implementation:
import sys
from PyQt4 import QtCore, QtGui, QtWebKit
class WebPage(QtWebKit.QWebPage):
def chooseFile(self, frame=None, path=''):
return QtGui.QFileDialog.getOpenFileName(self.parent(), '', path)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
view = QtWebKit.QWebView()
view.setPage(WebPage(view))
view.load(QtCore.QUrl('https://maps.google.com/'))
view.show()
sys.exit(app.exec_())

Is it possible to get QWebKit to display pdf files?

i have a link in my QWebkit, which points to a pdf file.
But when the link is clicked, it can't display the pdf file.
is there a way to make it happen?
If you enable plugins through QWebSettings and have a PDF viewer installed that provides a browser plugin such as Acrobat then you should see the PDF rendered using the plugin inside your QWebView:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.settings().setAttribute(QWebSettings.PluginsEnabled, True)
web.show()
web.load(QUrl('file:///C:/test/test.pdf')) # Change path to actual file.
sys.exit(app.exec_())
This code isn't working for me on Windows with the latest version of Acrobat X (it just shows a progress bar but no PDF - proof that the plugin is loading, just not working) but I'm sure this is how I've done it before. Give it a try and let me know.
Webkit doesn't include a PDF viewer. You'll need to have some way of rendering it - whether you pass it off to a different viewer (Adobe PDF viewer or something else), render it in the control in some way you devise (you could even try rendering it in JavaScript for fun).
This is Gary Hudges code for PyQt5:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtWebKitWidgets import *
from PyQt5.QtWebKit import *
from PyQt5.QtCore import *
app = QApplication(sys.argv)
web = QWebView()
web.settings().setAttribute(QWebSettings.PluginsEnabled, True)
web.show()
web.load(QUrl('file:///C:/data/progetti_miei/python/test.pdf')) # Change path to actual file.
sys.exit(app.exec_())

Categories

Resources