Hi I know this is a pretty basic design question. But I don't realy get it....
I write it in Python with PySide, but I think this is more a language unrelated question.
A simplified example what I want to do:
I Have a Gui with a button that opens a file dialog.
In this one I choose a folder.
The code scans the suffixes of the files in the folder and returns the 3 needed one. lets say .mp3, .txt and .mov and shows them in the gui.
To this point the seperation should be no problem I would have a Gui class that runs the code of the core class, gets the three files as return values and sets up the gui.
What I am wondering about is what happens when there are more then one files matching the .mp3 suffix. I would want to have a pop up with a combobox to select the one I want to use. But I don't realy get how to implement it without adding gui code to the core class.
Well maybe have the function in the Core module return some specifier that such a thing has happened (found multiple) along with the given names, then display the choice to the user and call a function in the Core module that returns relevant information about that file.
Bear in mind you do not have to be dogmatic regarding such restrictions, there are some situations where having code in the GUI is much less of a hassle than having to integrate some way of it to work in between modules.
This is where you make a decision how to go about writing the code, bearing in mind how important this feature is to you, how testable/maintainable you need it to be.
There are some applications that display XML structure in multiple views. An excellent example is Visual Studio Xaml ("Design") editor, which display an Object Tree, a rendered, "designer" window with mouse-manipulatable objects, a text editor with XAML content, and a properties window.
I am trying to do such a thing in PyQt to edit geographical maps, using KML as file format, but I would like to allow for direct editing and preservation formatting.
For example, I would like to be able to edit the formatting manually like this:
<Element attribute1="value1"
attribute2="value2"
attribute3="value3"/>
And be sure that attribute order and alignment would be kept intact upon further manipulation via TreeView or PropertiesWindow.
The fact is, a lot of answers say that, since attribute order is not a XML specification (that is, applications using XML should not count on it), the order is not guaranteed by any official library, and any library implementing it "is not doing XML". Specifically, not any Python library has support for this.
On the other hand, XAML editors in Visual Studio do that all the time.
So the question is: if I really, really want this, using Python GUI toolkits as a "support medium" (PyQt and its widgets, in principle), how hard would be the task? Should I expect to use python's XML libs, or would I have to write my own - that is, an overwhelmingly complex task - ?
I am trying to set up a print job in Python under Linux.
Now there is this GtkUnixPrintDialog that basically does what I need it to do, the problem is that I can't find any documentation on how to deal with it. I tried looking at the GTK3 reference docs, but that's all for C, and trying to translate those commands to Python didn't work.
I can easily get the dialog to open by adding it to my .glade file - but I can't find a way in Glade to connect the appropriate signals to it. Nor can I find a way to display it like other dialogs.
Anyone with a working example, to do a print job using the dialog?
Based on my previous question Creating print job in gtk3/python I have the code to set up the page as it has to be.
I am using reportlab libraries to create a pdf file, and that pdf file has to be printed out, preferably with a little control on which printer to use. Of course I could bypass that dialog entirely and dump it on the default printer, but the lack of documentation on that dialog irritates me as well.
It looks like GtkPrintUnixDialog is not included in introspection sources for GTK+. I am not sure if this is an oversight or done on purpose. However, this dialog is used internally by the higher level printing interface anyway which can be invoked with Gtk.PrintOperation.run by passing Gtk.PrintOperationAction.PRINT_DIALOG. There is a complete example in the pygobject sources.
My setup is PyQT4.10 (QT4.8.5) with Python 2.7 on Windows 7. Using pyuic4 to convert .ui files to Python codes always generates codes that use QtCore.QString.fromUtf8 and QtGui.QApplication.translate for string translation. Since there are only English characters in my programs, such translation is unnecessary and removing them can make the generated Python codes having a consistent style with the existing codes. It seems pyuic4 does not come with a switch to turn it off. Is there a way to disable this translation? Thanks.
No, you cannot turn this off.
The pyuic tool generates boiler-plate code and it needs to maintain backwards compatibility. So there are several aspects of these modules that differ from how you might write the same code yourself. To give another obvious example: with pyuic4, signals are connected using the old-style syntax, rather than the much more readable new-style syntax.
But really, why should you care about this? Obviously, there is never a good reason to edit these files yourself, and it is easy enough to exclude them from version control. So apart from ensuring that they are re-generated whenever appropriate, you can more or less forget that they are there.
If you have not installed a translator, the various tr functions are all effectively NO-OPs.
I've been learning python for a while now with some success. I even managed to create one or two (simple) programs using PyGtk + Glade.
The thing is: I am not sure if the best way to use GTK with python is by building the interfaces using Glade.
I was wondering if the more experienced ones among us (remember, I'm just a beginner) could point out the benefits and caveats of using Glade as opposed to creating everything in the code itself (assuming that learning the correct gtk bindings wouldn't exactly be a problem).
I would say that it depends: if you find that using Glade you can build the apps you want or need to make than that's absolutely fine. If however you actually want to learn how GTK works or you have some non-standard UI requirements you will have to dig into GTK internals (which are not that complicated).
Personally I'm usually about 5 minutes into a rich client when I need some feature or customization that is simply impossible through a designer such as Glade or Stetic. Perhaps it's just me. Nevertheless it is still useful for me to bootstrap window design using a graphical tool.
My recommendation: if making rich clients using GTK is going to be a significant part of your job/hobby then learn GTK as well since you will need to write that code someday.
P.S. I personally find Stetic to be superior to Glade for design work, if a little bit more unstable.
Use GtkBuilder instead of Glade, it's integrated into Gtk itself instead of a separate library.
The main benefit of Glade is that it's much, much easier to create the interface. It's a bit more work to connect signal handlers, but I've never felt that matters much.
Glade is very useful for creating interfaces, it means you can easily change the GUI without doing much coding. You'll find that if you want to do anything useful (e.g. build a treeview) you will have to get familiar with various parts of the GTK documentation - in practice finding a good tutorial/examples.
I started out using glade, but soon moved to just doing everything in code. Glade is nice for simple things, and it's good when you're learning how GTK organizes the widgets (how things are packed, etc). Constructing everything in code, however, you have much more flexibility. Plus, you don't have the glade dependency.
I usually start with Glade until I come to a point where it doesn't have the features I need, e.g. creating a wizard. As long as I'm using the standard widgets that Glade provides, there's really no reason to hand-code the GUI.
The more comfortable I become with how Glade formats the code, the better my hand-coding becomes. Not to mention, it's real easy to use Glade to make the underlying framework so you don't have to worry about all the initializations.
If you're writing a traditional GUI application which reuses a lot of standard components from GTK+ (buttons, labels, containers etc.) I'd personally go with Glade + Kiwi (a convenience framework for building GTK+ GUI applications).
The single greatest advantage to using Glade is that it greatly reduces layout/packing code. Here's an extremely simply example which already shows the issues with manually laying out a GUI (without using any helper functions):
container = gtk.HBox()
label = gtk.Label(str="test")
container.add(label)
For more examples take a look here. Even if you're writing a complicated custom widget you can always create a placeholder in Glade and replace that after instantiation.
It shouldn't be all too long now for the Glade team to release a new version of the designer (3.6.0). This new version will add support for GtkBuilder, which replaces libglade (the actual library that transforms the Glade XML files into a widget tree). The new Glade designer also once again adds support for defining catalogs (sets of widgets) in Python, so you can easily add your own custom widgets.
First, start to put this in perspective.
You will be using GTK. This is a huge C library built in 1993 using the best traditions of 1970s coding style. It was built to help implement the GIMP, a Photoshop competitor wanna-be with user interface blunders of legend. A typical gui field might have forty or more parameters, mostly repetitive, having getters and setters. There will be pain.
The GTK itself manages a complete dynamic type system in C using GObject. This makes debugging a special joy that requires manually walking through arrays of pointers to methods full of generic argument lists with implicit inheritance. You will also be jumping through Pango libraries when you least expect it, e.g., using a Pango constant for where in a label the ellipsis go when the page is small. Expect more pain.
By now, you are probably vowing to wrap all your GTK interactions in a Model-View-Controller architecture specific to your application. This is good.
Using Glade, or gtkBuilder, or Stetic, will help coral the huge coupling problem of forty parameters to a function. Glade provides a basic GUI builder to drag and drop components together. The parameters and inherited parameters are somewhat separated out. The output of Glade is .glade XML file which you will then read in, attach your callbacks ("signal handlers") to identically named functions, and query or update the in-memory version of that XML to get widgets that you then use pyGTK to manipulate. Glade itself is a creaky and not well maintained.
Using pyGTK gives you annoyingly fine grained control in order to build your GUI. This will be verbose, copy-and-paste code. Each attribute will be a separate function call. The attribute setter does not return anything, so chaining the calls is out of the question. Usually, your IDE will give only minimal help on what functions mean and you will be constantly referring to DevHelp or some other tool.
One would almost expect GTK GUIs were meant to fail.
I recommend using Glade for rapid development, but not for learning. Why? because some times you will need to tune up some widgets in order to work as you want they to work, and if you don't really know/understand the properties attributes of every widget then you will be in troubles.
For quick and simple screens I use Glade. But for anything that needs finer levels of control, I create a custom classes for what I actually need (this is important, because it's too easy to get carried away with generalisations).
With a skinny applications specific classes, I can rapidly change the look and feel application wide from a single place. Rather like using CSS to mantain consistency for web sites.
Personally I would recommend coding it out instead of using Glade. I'm still learning python and pyGtk but I will say that writing out the UI by hand gave me a lot of insight on how things work under the hood.
Once you have it learned I'd say to give glade, or other UI designers a try but definitely learn how to do it the "hard" way first.
You may use glade-2 to design, and use glade2py.py to generating the pure pygtk code,
it use pygtkcompat to support gtk3