I'm very new to python and i have some stupid question, thank you for helping!
when I search online about wxpython questions, I see almost 100% of the people use class for wxpython. From the book i learnt, the author didn't mention using class for wxpython, instead he gave us a few examples that didn't use class.
I just want to know, when is it good to use class and when is it really not so necessary.
You will need to use a class to overwrite the behavior of an existing Widget.
Many tutorials online start by using a custom extension of wx.Frame as the base window for the application, but in fact there is no real reason to do that.
If you are satisfied with everything that widgets normally provide out of the box there is no reason to use classes - in fact you should avoid Object Oriented Programming as much as possible. Using a more functional approach will result in cleaner code and less troubles.
In wxPython, you should use classes whenever you want to extend the functionality of something wxPython provides to you.
A lot of wxPython apps at the very least define a class inheriting from wx.Frame to serve as the main GUI. If you need to add special behavior to certain widgets (e.g. add a method to wx.Button), you might also want to write a class for that. Lastly, some special features in wxPython (like tabbed GUIs or wizards) require you to write a class for each page.
There's a lot of cases where you need to write a class. You almost always need to at least write one class for your main frame. Other than that, it depends on what kind of problems you're trying to solve.
I'm not sure what book you read, but if you haven't already I'd recommend wxPython in Action, which is really the definitive guide to wxPython.
Related
I need to find global caret position in Linux. The problem is similar to this one for Windows. Do you guys have any idea?
More information:
I am trying to make input method for a certain Indic language. I am using IBus libraries in Python. I need to create something like the lookup table found in IBus but my requirements are such that I decided its better if I make the whole thing again using tk (or something). The link in the question solves this problem for windows where a tooltip follows the text caret. So I need something just like that but for X-Windows.
There is no such thing as a caret position in X11. While the older UIM framework did a fairly good job of displaying the input method UI near the cursor position, this failed often enough that it was abandoned.
You might want to take a look at the SCIM framework. Note that it is usually preferred to hint the application at the completion state rather than provide a separate editor, as this gives a more seamless integration.
I figured it out! All I had to do was create a method in my IBus engine class (sub class of IBus.Engine) called do_set_cursor_location which handles the signals created when position of the caret changes. Here is more from IBus manual: The "set-cursor-location" signal.
That means the problem is solved for now, although I certainly don't know what is happening under the hood.
Thanks guys.
Actually working on a project in python with PyQT, we choose to create widgets that were "unpleasant" or that didn't have a good enough behaviour.
So, we finally found that QToolbox, QDate and some others had a behaviour non acceptable for the project, so we had to adapt these.
We had also to create a complete new widget : A scheduler.
As we were creating these, it has been decided that it took too much time. So we were asked to think about other libraries.
I actually found a project of a scheduler in wxPython, that actually looks like what we want ( but we believe that we'll have to adapt it a lot ). Here it is : http://code.google.com/p/wxscheduler/
So, I ask everyone that have some more experience than me in GUI programming in python : Do we need to start again the project in anything other than PyQT? I know the question is weird, but what you need to know is :
The project has now been going on for 2 months
I know only PyQT, and started working in python 2 month ago
We are currently 3 in the project, and we currently know only PyQT
We have currently managed a lot of the PyQT widgets, and were starting to code these new widgets.
Please help us =)
Thanks
Edit : I should have add that the project is opensource and multi-platform
Feel free to look at other libraries if you like. Robin Dunn, the creator of wxPython, recently started working on PySide and he found it somewhat similar to wx, so you might find that wxPython will fit your brain fairly well too. I certainly think wx's class names are more intuitive than PyQt's. The only way to know for certain is to actually experiment a little and see if it works. I will say that the wxPython community is one of the best Python communities I've dealt with over the years.
One possibility would be to use an HTML scheduler control via QtWebKit. If your UI can accommodate a QWebView in the place where you'd otherwise have a custom scheduler widget, there are probably a number of excellent scheduler widgets (implemented as jQuery plugins, etc.) you could choose from.
we choose to create widgets that were "unpleasant" or that didn't have
a good enough behaviour.
Why don't you create "pleasant" widgets ?
so we had to adapt these.
Yes. It is the solution.
it has been decided that it took too much time
Don't you think it would take much more time if you change the whole GUI API ?
As for i know, there is not such native Scheduler in any Python GUI library, especially one you could use with Qt. I don't think it would be so long to recreate one, unless you have very specific needs, that would confirm you wouldn't find a such existing thing in an existing library.
Concerning wxScheduler, i guess you can have a look to the code, even it uses wxWidget and you're working with Qt, to get an idea how to do it.
When starting up a new project, as a beginner, which would you use?
For example, in my situation. I'm going to have a program running on an infinite loop, constantly updating values. I need these values to be represented as a bar graph as they're updating. At the same time, the GUI has to be responsive to user feedback as there will be some QObjects that will be used to updated parameters within that infinite loop. So these need to be on separate threads, if I'm not mistaken. Which choice would give the most/least hassle?
If I understood your question correctly, updating the GUI has a little to do with the way you programmed it.
From my experience, it's easier to design a main window (or whatever your top level object is) in Designer, and add some dynamically updated content in a widget(s) created in your code. In most cases, it saves your time spent on digging through QT documentation, and additionally, you are able to visually inspect positioning, aligning etc.
You don't lose anything by using a Designer, every part of the GUI can be modified in your code afterwards, if it needs some custom behavior.
Having said that, without knowing all the details of your project is hard to tell which option (QT or in-code) is faster.
Your right threading is your answer. Use the QT threads they work very well.
Where I work when people start out using QT a lot of them start with designer but eventually end up hand coding it. I think you will end up hand coding it but if you are someone who really likes GUIs you may want to start with Designer. I know that isn't a definitive answer but it really depends.
First of all, the requirements that you've mentioned don't (or shouldn't) have much affect on this decision.
Either way, you're going to have to learn something. You might as well investigate both options, and make the decision yourself. Write a couple of "Hello, World!" apps, then start adding some extra widgets/behavior to see how each approach scales.
Since you asked, I would probably use Qt Designer. But I'm not you, and I'm not working on (nor do I know much of anything about) your project.
The tutorials I've found on WxPython all use examples from Linux, but there seem to be differences in some details.
For example, in Windows a Panel behind the widgets is mandatory to show the background properly. Additionally, some examples that look fine in the tutorials don't work in my computer.
So, do you know what important differences are there, or maybe a good tutorial that is focused on Windows?
EDIT: I just remembered this: Does anybody know why when subclassing wx.App an OnInit() method is required, rather than the more logical __init__()?
I've noticed odd peculiarities in a small GUI I wrote a while back, but it's been a long time since I tried to the specifics are a rather distant memory. Do you have some specific examples which fail? Maybe we can improve them and fix the bugs?
Have you tried the official wxPython tutorials? ...or were you after something more specific?
r.e. your edit - You should use OnInit() because you're subclassing wx.App (i.e. it's a requirement for wxWidgets rather than Python) and the wxPython implementation is wherever possible, just a wrapper for wxWidgets.
[Edit] Zetcode has a fairly lengthy tutorial on wxPython. I've not looked through it all myself, but it might be of some help?
The wxWidgets::wxApp::OnInit() documentation is fairly clear:
This must be provided by the application, and will usually create the application's main window, optionally calling wxApp::SetTopWindow. You may use OnExit to clean up anything initialized here, provided that the function returns true.
If wxWidgets didn't provide a common interface then you'd have to do different things in C++ (using a constructor) compared to Python's __init__(self,...). Using a language-independent on-initialisation allows wxWidgets ports to other languages look more alike which should be a good thing right? :-)
EDIT: I just remembered this: Does anybody know why when subclassing wx.App an OnInit() method is required, rather than the more logical __init__()?
I use OnInit() for symmetry: there's also an OnExit() method.
Edit: I may be wrong, but I don't think using OnInit() is required.
I find a number of small differences, but don't remember all of them. Here are two:
1) The layout can be slightly different, for example, causing things to not completely fit in the window in one OS when the do in the other. I haven't investigated the reasons for this, but it happens most often when I use positions rather than sizers to arrange things.
2) I have to explicitly call Refresh more in Windows. For example, if you place one panel over another, you won't see it the top panel in Windows until you call Refresh.
I general, I write apps in Linux and run them in Windows, and things work similarly enough so this is a reasonable approach, but it's rare for me when something runs perfectly straight out of the gate after an OS switch.
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