PyQt Center on Screen

wxPython has been annoying me with the inconsistent behavior of its splitter window. Perhaps I’m doing something wrong, but the framework in general isn’t exactly fun to develop in. I’ve since discovered PyQt and while it’s not exactly Pythonic, it seems to make more sense. However, if you’re moving from wxPython to PyQt, you might be aware that there’s no obvious method of centering the main window on the screen. I’ve seen at least one method so far (to which I replied as my alter ego, Zancarius), but it doesn’t take into account something fairly minor but of some significance:

Windows have borders around them in most desktop environments!

Window borders can range from fairly minor (4 pixels around each side adding a total of 8 pixels horizontally and vertically) to imposing (16 pixels or more). Unfortunately, QWidget.geometry() and friends only return a QSize object describing the geometry of the contained window rather than the containing window. It seems insignificant, sure, but if you want pixel-perfect alignment, you have to use something else:


frameSize() will return the size of the entire window, border included. Here’s a method you can include in your own classes as in this example:

class ExampleWindow (QtGui.QMainWindow):
    def __init__ (self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.setGeometry(0, 0, 650, 550)
        self.setWindowTitle("My Example Application")
    def centerOnScreen (self):
Centers the window on the screen.'''
        resolution = QtGui.QDesktopWidget().screenGeometry()
        self.move((resolution.width() / 2) - (self.frameSize().width() / 2),
                  (resolution.height() / 2) - (self.frameSize().height() / 2))

centerOnScreen() works by taking the desktop’s current resolution and dividing it by 2 to get half of the viewport. Next, it takes half of the window size (including the border) and subtracts that from the viewport half. This provides an origin for the upper-left corner of the window to be located such that the window itself is fully “centered.”

Simple, huh?

No comments.

Links: June 24th

I’m a little late this time around.

  • PyPy’s developers appear to have given it a boost and it’s now 50% faster than CPython in some tests. ‘Course, this is development code only, but I think it’s pretty impressive for what essentially mounts to a self-hosted Python interpreter.
  • The German Tank Problem, explained. Yeah, it may be a pointless read for some, but I found it to be rather fascinating–and I’m not a math guy. This is the kind of real world stuff they ought to use in schools.
  • Smashing Magazine has a fascinating article on 10 UI design patterns you should be paying attention to. It’s definitely worth a read for those of you with a knack for design. (I’m not exactly one of those types, but this is definitely worth a read for self-improvement. I know I’ll be putting these tips to good use in the future.)
  • Besides the notion of China having a lottery, there are stranger coincidences when dealing with impressively large populations. If true, that has got to be the weirdest thing I’ve read.
  • I couldn’t quite get the latest version of Py2EXE working without totally trashing distutils every time it attempted to run. I have found a suitable replacement called PyInstaller. It seems to work well and with minimal fuss. I like it. Oh, and you can bundle packages for most operating systems (though, I’d suggest looking at Freeze for *nix-specific distribution). I’m not sure how well it handles .eggs, though.
  • Ever been curious about the release timeline of different web browsers including some you may have never heard of? Take a look at this chart on Wikipedia.
No comments.

Quickies: Telnet in Cygwin 1.7

Windows’ cmd.exe is pretty anemic and just doesn’t have the feel of a real command line. (It isn’t.) PowerShell is cute and has its uses for poking around with COM objects and the likes, but for common tasks it seems ridiculously verbose and unnecessary. Cygwin alleviates much of this in an Windows environment and grants those of us who use proper shells a method of interfacing with Windows. Well, kind of!

Unfortunately, Cygwin 1.7 has moved a few things around. Geeks like me tend to use telnet to verify connectivity to other hosts, communicate directly with certain services (hey, HTTP isn’t that difficult), and troubleshoot. However…

[gridlock-x:~]$ which telnet

Hmmmm… this isn’t good. Windows’ telnet is terrible. More importantly, it doesn’t work in Cygwin. After some exploration, it would seem that the Cygwin folks have consolidated telnet and a few other useful utilities into a single package.


If your distribution doesn’t have telnet available, make sure to run the setup utility again and select the “inetutils” package. Once you’ve finished up the installation (again), it’ll install only the changed package, and you’ll have access to telnet again. Horray!

Although Cygwin 1.7 is still in beta, the developers are encouraging everyone to give it a try. What can I say? It has a lot of nice improvements over the 1.5 branch. Heck, it even finally comes with a decent icon for a pre-configured rxvt shortcut! Now that’s classy.

Try it out!

No comments.