Results 1 to 8 of 8

Thread: Python popup textbrowser

  1. #1
    Join Date
    Feb 2015
    Posts
    27
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Python popup textbrowser

    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?

  2. #2
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: Python popup textbrowser

    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-M...er/helpform.py.

  3. #3
    Join Date
    Feb 2015
    Posts
    27
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Python popup textbrowser

    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.

    Qt Code:
    1. self.textBrowser = QtGui.QTextBrowser()
    2. self.textBrowser.setHtml(QtGui.QApplication.translate('Dialog','Help Menu', None, QtGui.QApplication.UnicodeUTF8))
    3. self.textBrowser.show()
    To copy to clipboard, switch view to plain text mode 
    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.

  4. #4
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: Python popup textbrowser

    Look at the example. You can subclass QWidget. It will have a close button automatically. As for file I/O : http://stackoverflow.com/questions/1...ng-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.

  5. #5
    Join Date
    Feb 2015
    Posts
    27
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Python popup textbrowser

    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:

    Qt Code:
    1. text_edit = QPlainTextEdit()
    2.  
    3. text=open('file.txt').read()
    4. text_edit.setPlainText(text)
    To copy to clipboard, switch view to plain text mode 

    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...

  6. #6
    Join Date
    Jun 2014
    Posts
    98
    Thanks
    43
    Thanked 4 Times in 4 Posts
    Platforms
    Windows

    Default Re: Python popup textbrowser

    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.

  7. The following user says thank you to neuronet for this useful post:

    ChrisOfBristol (20th February 2015)

  8. #7
    Join Date
    Feb 2015
    Posts
    27
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Python popup textbrowser

    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.

    Qt Code:
    1. self.text_edit = QtGui.QTextEdit()
    2. text=open('/home/chris/Documents/Linux/Qt/image3d.html').read()
    3. self.text_edit.setHtml(text)
    4. self.text_edit.show()
    To copy to clipboard, switch view to plain text mode 

    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-doc...it.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.
    Last edited by ChrisOfBristol; 20th February 2015 at 20:58.

  9. #8
    Join Date
    Feb 2015
    Posts
    27
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Python popup textbrowser

    It's now working and here is the code fragment:
    Qt Code:
    1. def actionHelpTriggered(self):
    2. with open('helpfile.html', 'r') as datafile:
    3. result = datafile.read()
    4. self.helptext = QtGui.QTextEdit()
    5. self.helptext.setHtml(result)
    6. self.helptext.setReadOnly(-1)
    7. self.helptext.resize(800, 500)
    8. self.helptext.show()
    To copy to clipboard, switch view to plain text mode 

    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.

Similar Threads

  1. Embedding PyQt4 into an Application running Python via Boost::Python
    By GreyHound in forum Installation and Deployment
    Replies: 1
    Last Post: 6th February 2012, 06:48
  2. Replies: 12
    Last Post: 1st May 2011, 11:38
  3. Replies: 0
    Last Post: 8th April 2011, 20:10
  4. How to make a popup widget that looks like a popup?
    By Olivier Berten in forum Qt Programming
    Replies: 9
    Last Post: 6th June 2010, 13:19
  5. Replies: 3
    Last Post: 17th May 2009, 20:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.