Question

I am writing some code to obtain the size of the physical screen and use those dimensions to resize my window :

#!/usr/bin/env python

import gtk

class GettingStarted:
    def __init__(self):
        window = gtk.Window()
        width = gtk.gdk.Screen.get_width()#line1
        height = gtk.gdk.Screen.get_height()#line2
        window.resize(width,height)#line3
        label = gtk.Label("Hello")
        window.add(label)

        window.connect("destroy", lambda q : gtk.main_quit())
        window.show_all()

GettingStarted()
gtk.main()

With line1,line2,line3 commented out of the code, a regular window with "Hello" is displayed on screen. But with the aforesaid lines included in the code, a calendar is displayed for some reason! Also an error is thrown :

Traceback (most recent call last):
  File "gettingstarted.py", line 17, in <module>
    GettingStarted()
  File "gettingstarted.py", line 8, in __init__
    width = gtk.gdk.Screen.get_width()
TypeError: descriptor 'get_width' of 'gtk.gdk.Screen' object needs an argument

There's no mention of any arguments for get_width() or get_height() in the docs.What's happening?

Was it helpful?

Solution

You are using a class instead of an instance in two locations, line1 and line2, try gtk.gdk.screen_get_default() instead of gtk.gdk.Screen in both locations.

#!/usr/bin/env python

import gtk

class GettingStarted:
    def __init__(self):
        window = gtk.Window()
        width = gtk.gdk.screen_get_default().get_width()#line1
        height = gtk.gdk.screen_get_default().get_height()#line2
        window.resize(width,height)#line3
        label = gtk.Label("Hello")
        window.add(label)

        window.connect("destroy", lambda q : gtk.main_quit())
        window.show_all()

GettingStarted()
gtk.main()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top