Text tree in Python GUI - python

I'm looking for GUI feature that will be part of Python desktop application.
The feature is a text tree with drag and drop functionality between individual nodes.
Further I'd like to be able to attach a note with longer text to individual nodes in the tree. The note would be switchable to expanded or collapsed state.
It is exactly shown in this NestedSortable JQuery library except of the switchable note.
Could you show me what are the possibilities of contemporary Python GUIs according to this feature? I prefer lightweight GUI with modern look as in the JQuery example.
Which GUI would be the most suitable for this task? Tkinter, wxPython, pyQt, pyGTK or other? Or would you choose some GUI + Javascript libraries? Why would you prefer particular GUI to achieve requested functionality and lightweight modern design?

pyQT has a good solution for this with its QTreeWidget or a MVC setup. QTreeWidget is simplest:
Here's a small example using PyQt4
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.treewidget = QTreeWidget(self)
self.treewidget.setHeaderLabels(['a'])
self.treewidget.setDragEnabled(True)
self.treewidget.setAcceptDrops(True)
self.treewidget.setDropIndicatorShown(True)
self.treewidget.resize(300,300)
self.titems = []
for i in xrange(100):
if not i%10:
pitem = QTreeWidgetItem(self.treewidget,["Parent %d"%i])
self.titems.append(pitem)
else:
item = QTreeWidgetItem(pitem,["Child of %d"%i])
self.titems.append(item)
self.show()
app = QApplication(sys.argv)
w = MainWindow()
app.exec_()

Try using etetoolkit. Its much simpler.

Related

pyside embed vim

I know to embed vim in a Gtk application using sockets like the following snippet
from gi.repository import Gtk
import subprocess
win=Gtk.Window()
win.set_default_size(600,800)
win.connect('delete-event', Gtk.main_quit)
editor = Gtk.Socket()
win.add(editor)
editor.connect("plug-removed", Gtk.main_quit)
subprocess.Popen(["/usr/bin/gvim", \
"--socketid", str(editor.get_id())])
win.show_all()
Gtk.main()
How does one do this in PySide? I could not find any reference to sockets in pyside.
UPDATE (using JimP's idea)
The following code embeds a gvim instance in a Pyside widget. However the gvim window does not seem to resize when to the full size of the parent window.
import sys
from PySide import QtGui
from PySide import QtCore
app = QtGui.QApplication(sys.argv)
win = QtGui.QWidget()
win.resize(600, 800)
container = QtGui.QX11EmbedContainer(win)
container.show()
QtCore.QObject.connect(container,
QtCore.SIGNAL("clientClosed()"),
QtCore.QCoreApplication.instance().quit)
winId = container.winId()
process = QtCore.QProcess(container)
options = ["--socketid", str(winId)]
process.start("gvim", options)
win.show()
sys.exit(app.exec_())
I think the key to getting this working would be translating GTK speak to QT speak. Google around your code, I see that Gtk.Socket says:
The communication between a GtkSocket and a GtkPlug follows the XEmbed
protocol. This protocol has also been implemented in other toolkits,
e.g. Qt, allowing the same level of integration when embedding a Qt
widget in GTK or vice versa.
So then the question becomes what does QT call their XEmbed classes? Google around I found QX11EmbedContainer which says:
It is possible for PySide.QtGui.QX11EmbedContainer to embed XEmbed
widgets from toolkits other than Qt, such as GTK+. Arbitrary
(non-XEmbed) X11 widgets can also be embedded, but the XEmbed-specific
features such as window activation and focus handling are then lost.
The GTK+ equivalent of PySide.QtGui.QX11EmbedContainer is GtkSocket.
The corresponding KDE 3 widget is called QXEmbed.
I'm not running PySide at the moment, but that page on QX11EmbedContainer contains some example C++ code that I think will get you where you need to be. You will need to translate the C++ to Python, but I don't that will be too hard.

Can I create a panel-like thing on Windows with Python?

Sorry for the vague title, couldn't come up with anything more informative %)
What I want is a 5px horizontal panel on the top of the screen that I can draw on (and, possible, handle clicks on too).
One of the following features would be awesome (although I understand it's probably not really possible to combine both of them):
the panel should be just like the Windows's own taskbar, i.e., maximized windows should not overlap it, but start below it instead
the panel should show in fullscreen apps too
Is it possible to do this in Python?
Thanks.
Yes, it's possible. The "how" part depends on the GUI library you choose for which there are many options, but most people will recommend the following two: wxPython or PySide which is Qt for Python.
PySide has good documentation and tutorials.
What you will want to do is create a QMainWindow instance and set the WindowFlags to your requirements. You probably want the following combination Qt::Window | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint.
Something like this:
import sys
from PySide.QtCore import *
from PySide.QtGui import *
class Form(QMainWindow):
def __init__(self, parent=None):
super(Form, self).__init__(parent)
self.setWindowFlags(Qt::Window | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint)
if __name__ == '__main__':
# Create the Qt Application
app = QApplication(sys.argv)
# Create and show the form
form = Form()
form.show()
# Run the main Qt loop
sys.exit(app.exec_())
Note, that there is a limit to the "staying on top" nature of such windows. There are Win32-specific ways to fight it and get even higher, but such requirement would be a design error.

are there any cross platform window toolkits for python that aren't made by crazy people?

well maybe crazy is a bit too strong of a word, but what I am asking is if there are any window toolkits out there that don't have me do this:
class MyApp(SomeWindowClass):
I really don't want to use a library made by someone who is so obsessed with objects that he/she thinks that there should be a class for the app (which there will only be one instance of, so I don't see why anyone would want to do that extra typing)
(btw, no offense intended towards anyone who agrees with the way these libraries are set up, I just really want to know if there is anything out there with a tad bit less objects)
In general GUI toolkits rely on having some form of event loop running, the Application class in these toolkits is generally in charge of that event loop and marshaling events from the underlying window manager. Sure they could call the class EventLoopManager or something, but you need it either way so its just a naming thing then. In some cases though some toolkits who often use events can occasionally be used without them, and then you certainly dont want it to be some automatic thing.
There is PyQT.
Tkinter has one object per window/dialog, not app, and requires no classes to get something painted on the screen. It does, however, have its own main loop (like all the other GUI libraries). Obligatory Hello World:
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
PyGTK another toolkit; which is python binding of Gtk. It is well structured with having excellent window and event loop system.
A typical example to show a window
import gtk
class Application:
def __init__(self):
self.window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
self.window.show()
if __name__ == "__main__":
app = Application()
gtk.mainloop()
Another recommendation is PyQt; which is python binding of Qt.
Typical hello world example is
import sys
from qt import *
app = QApplication(sys.argv)
myLabel = QLabel("Hello world!", None)
app.setMainWidget(myLabel)
myLabel.show()
app.exec_loop()
PyQt and PyGtk are widely used for rapid GUI development. From my experience pyGtk is poor in the online documentation/support compared to pyQt. But both are favorite of mine.
As you see with the answers above, GUI programming is almost always heavily object oriented, and there are good reasons for this: the graphical elements share a lot in terms of how they can be positioned within one-another, caring about whether the mouse pointer is over them etc. Furthermore, the C++ kits that qt, wx, gtk et al. wrap are already structured on a class/inheritance hierarchy, so you should not be surprised that the python wrappers are also.
If you want only simple GUI elements, then you may consider easyGUI (simple message boxes, text edit, choices), triatsUI (interactive object wrappers, primarily for controlling graphical objects) , which each solve some part of the GUI interactions without explicitly having you write GUI code.
For editing the values of fields in a record-like structure, you could also investigate GUIdata.
PS: there are various graphical tools out there to let you design your GUIs and link together some of the events, e.g., QtDesigner, that can help you avoid much of the tedious class definition code.

Cross-platform Python GUI suitable for taskbar (Win) and menubar (mac) functionality?

I am fairly new to Python programming, and completely new to cross-platform GUI building (only previous GUI experience is through visual basic and Java).
I've written some python code to screen-scrape data from a website, and now I want to build a GUI that will reside in the Mac OS X menubar, and in Window's task bar (i.e., the system tray).
The most useful general page on cross-plaform Python GUIs for me was this one (despite its name indication Window GUIs). And some stackoverflow questions came in useful as well (especially this one, and the accepted answer of this one about splitting up the GUI and cli code).
I think I will go for either wxPython or QT because I want the GUI to look as native as possible.
However, as I've said the fairly simple GUI will mainly live in the taskbar/menubar.
Should this influence my decision?
Here's an example for PyQt. This works for me on MacOS X; I haven't tried it on other platforms. Note that the QSystemTrayIcon class will raise exceptions if it doesn't have an icon – I grabbed the RSS feed svg from Wiki commons for my icon.svg (but you can give QIcon a PNG directly and not mess around with QtSvg).
import PyQt4
from PyQt4 import QtCore, QtGui, QtSvg
app = QtGui.QApplication([])
i = QtGui.QSystemTrayIcon()
m = QtGui.QMenu()
def quitCB():
QtGui.QApplication.quit()
def aboutToShowCB():
print 'about to show'
m.addAction('Quit', quitCB)
QtCore.QObject.connect(m, QtCore.SIGNAL('aboutToShow()'), aboutToShowCB)
i.setContextMenu(m)
svg = QtSvg.QSvgRenderer('icon.svg')
if not svg.isValid():
raise RuntimeError('bad SVG')
pm = QtGui.QPixmap(16, 16)
painter = QtGui.QPainter(pm)
svg.render(painter)
icon = QtGui.QIcon(pm)
i.setIcon(icon)
i.show()
app.exec_()
del painter, pm, svg # avoid the paint device getting
del i, icon # deleted before the painter
del app
See this related SO answer on how to accomplish Windows system tray/OS X menu bar functionality in wxPython.

how to create windows 7 jump lists via python/pyqt?

i have a pyqt project which i'm interested in using to play around with the new windows 7 jump list feature. after a bunch of searching, i have not found any specific examples of anyone creating jumplists via python.
has anyone here found an easy way to hook into this? does mark hammond's pywin32 module have an appropriate wrapper?
thanks!
I don't think Qt supports jump lists, you can find a bit more info here
Qt 4.6 added support for windows 7 and it was released today but I don't think they added this specific feature and I don't think PyQt supports this release.
There is a Qt add-on that implements all the Windows 7 taskbar extensions. It is called Q7Goodies. Although, it is a C++ library, I suggest contacting the authors, maybe they provide a PyQt bindings too.
Ah well after 12 years Imma answer this XD.
There is an add-on introduced in Qt 5.2 named "QtWinExtras/Windows Extras". As explained by the docs:
Qt Windows Extras provide classes and functions that enable you to use miscellaneous Windows-specific functions. For example, you can convert Qt objects to Windows object handles and manipulate DWM glass frames.
In addition, you can use features introduced with Windows 7, such as Aero Peek, Jump Lists, a progress indicator on a taskbar button, or a thumbnail toolbar.
You can check the code bellow as an example:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
from PyQt5.QtWinExtras import QWinJumpList, QWinJumpListItem
class Form(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.resize(350, 150)
self.setWindowTitle("Windows Jump Lists")
label = QLabel("Right click the taskbar button")
label.resize(label.sizeHint())
label.setAlignment(Qt.AlignCenter)
self.setCentralWidget(label)
jump_list = QWinJumpList()
tasks = jump_list.tasks()
taskmgr = QWinJumpListItem(QWinJumpListItem.Link)
taskmgr.setTitle("Open Task Manager")
taskmgr.setFilePath("C:\\Windows\\system32\\taskmgr.exe")
tasks.addItem(taskmgr)
tasks.setVisible(True) # Necessary
self.show()
app = QApplication(sys.argv)
form = Form()
sys.exit(app.exec_())
You can check some examples here.
For more information you can check the official documentation

Categories

Resources