PDA

View Full Version : Python popup textbrowser



ChrisOfBristol
20th February 2015, 18:36
This is my first PyQt app and my first Python program.
I have a Help option in the usual place on the top menu bar. I want it to popup a textbrowser which will load an html file and have a scrollbar and a close button, but these things are fixed open on the window and have no buttons. A popup messagebox might do but it has no way of loading the text from the file and no scrollbar. I need something like a combination of the two. I find it very difficult working out how to create these things, so trial and error is taking for ever. Is there such a widget, perhaps with parameters to tailor it?

neuronet
20th February 2015, 19:19
I'm not sure I fully understand what you want, but as discussed in Chapter 17 of Summerfield's PyQt book, you can open a QWidget that includes a QtGui.QTextBrowser() to display html. You can find helpform.py at his website, or other code inspired by his code, like here: https://github.com/konchris/Sample-Manager/blob/master/helpform.py.

ChrisOfBristol
20th February 2015, 19:45
That was a quick answer! I've cobbled this together from bits I've found on a couple of sites, it works(!) and is nearly what I want.


self.textBrowser = QtGui.QTextBrowser()
self.textBrowser.setHtml(QtGui.QApplication.transl ate('Dialog','Help Menu', None, QtGui.QApplication.UnicodeUTF8))
self.textBrowser.show()
I would also like it to have a close button and to load its text from a file, are there parameters for this? I'll have a look for some documentation, but I generally find it impenetrable and it doesn't seem to be for when you've got a ready made GUI from Qt.

neuronet
20th February 2015, 20:01
Look at the example. You can subclass QWidget. It will have a close button automatically. As for file I/O : http://stackoverflow.com/questions/11011294/pyqt-reading-a-text-file

An introductory, basement-up intro to PyQt would get at most of these issues, so I recommend Summerfield's book. His helpform example is chapter 17.

ChrisOfBristol
20th February 2015, 20:19
I'm afraid that example went right over my head - and I have no idea what to 'subclass' means. The code from the second example:


text_edit = QPlainTextEdit()

text=open('file.txt').read()
text_edit.setPlainText(text)

doesn't seem to do anything. This is the last problem to solve to finish my app, so I'm disinclined to read a whole book, I'll keep experimenting...

neuronet
20th February 2015, 20:24
Sounds like a good idea: a complex PyQt GUI is not a great way to start out with Python...it is a framework that takes a pretty serious commitment to get good at it. I say that as someone who is just a novice after 6 months studying it just about every day. It is easy to do simple things, but complex things you need more than you will find by Googling. Especially in Python, as PyQt/PySide documentation aren't very good compared with what you will find with straight-up Qt. Not to be discouraging, but just a heads up. If you are not sure what 'subclassing' is, that suggests your time would be much better spent focused on Python and object-oriented programming, and then if you need PySide/PyQt you will be better prepared for it.

ChrisOfBristol
20th February 2015, 20:32
Fair point. I'm used to procedural languages and can learn them quickly, so that side of Python is no problem for me. The object orientation does my head in so I intend to avoid it as far as is possible! Unfortunately it's necessary as part of Qt. I've only created a very simple app and have other things to do, so am unlikely to want to take this much further. I've already spent too much time writing computer programs... Thanks anyway.


self.text_edit = QtGui.QTextEdit()
text=open('/home/chris/Documents/Linux/Qt/image3d.html').read()
self.text_edit.setHtml(text)
self.text_edit.show()

Got it! Some of the statements needed "self." in front of them, I've no idea why, probably something to do with obje*** orient*****! Also removing Plain from QPlainTextEdit and changing setPlainText to setHtml got me the HTML I wanted.

I've also worked out that self.text_edit.setHtml(text) is a way of setting parameters, and I can make it read-only with self.text_edit.setReadOnly(-1). The documentation here http://srinikom.github.io/pyside-docs/PySide/QtGui/QTextEdit.html#PySide.QtGui.PySide.QtGui.QTextEdit .setReadOnly seems more undestandable to me than what I've found on the PyQt site, even if it is for PySide which I'm not using.

I'll post the complete code fragment when I've finished it in case it's useful to another complete novice.

ChrisOfBristol
26th February 2015, 23:39
It's now working and here is the code fragment:

def actionHelpTriggered(self):
with open('helpfile.html', 'r') as datafile:
result = datafile.read()
self.helptext = QtGui.QTextEdit()
self.helptext.setHtml(result)
self.helptext.setReadOnly(-1)
self.helptext.resize(800, 500)
self.helptext.show()

It hasn't got an [OK] button and it would be better if it had to be closed before the main window is, but it is just about adequate.