Question

I have this gtk.DrawingArea placed in gtk.Window, but when it runs its not 100% scaled fit with gtk.Widnow height and width.

enter image description here

Following code is not doing the 100% width/height scale fit to the gtk.Window. Any idea how to get the expected result?

  def main(self, screenSaverPicture):    

    self.w = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.w.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(0, 0, 0))

    self.w.set_size_request(1024, 768)

    self.w.set_border_width(0)
    self.w.set_decorated(False)
    self.w.set_title("Screen Saver Screen Locked")        
    self.w.move(0,0)
    #self.w.resize(66,66)

    self.w.set_name("main window")    

    # screenSaverPicture = pictures png or jpeg files shown on the screen 
    self.drawing =  gtk.DrawingArea
    self.drawing.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color('#000000'))   

    self.drawing.connect ('button-press-event',self.callback)
    self.drawing.set_events(gtk.gdk.EXPOSURE_MASK | gtk.gdk.LEAVE_NOTIFY_MASK | gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.POINTER_MOTION_MASK| gtk.gdk.POINTER_MOTION_HINT_MASK )

    self.w.add(self.drawing)

    self.w.show_all()
    self.w.connect("destroy", gtk.main_quit)
    gtk.main()

EDIT: Tried but also same

self.vbox = gtk.VBox() 
self.vbox.pack_start(self.drawing, expand=True) 
#or self.vbox.pack_start(self.drawing, expand=False) 
self.w.add(self.vbox)

or

self.vbox = gtk.Fixed()
self.vbox.put(self.drawing, 0,0)
self.drawing.size_request()
self.drawing.size_allocate(gtk.gdk.Rectangle(0,0,1024,768))  
self.w.add(self.vbox)
Was it helpful?

Solution

Please next time, provide a runnable example, it's boring to have to write the missing parts and comment what is unrelated.

Here's an example that should work. Basically, the code you provide doesn't even have the problem you say you have. The main problem of your initial sample is that you're setting a black background to your GtkDrawingArea, instead of a blue one.

import gtk

class Test:

    def main(self):
        self.w = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.w.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(0, 0, 0))
        self.drawing =  gtk.DrawingArea()
        self.drawing.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color('#0000ff'))   
        self.w.add(self.drawing)
        self.w.show_all()
        self.w.connect("destroy", gtk.main_quit)
        gtk.main()

if __name__ == '__main__':
    a = Test()
    a.main()

OTHER TIPS

Don't set static size, and don't draw in main or __init__

Instead, fetch the size in expose-event callback, and draw in that callback depending on the current size of widget.

Something like this (your code was incomplete so I couldn't test it)

  def main(self, screenSaverPicture):    

    self.w = gtk.Window(gtk.WINDOW_TOPLEVEL)

    self.w.set_border_width(0)
    self.w.set_decorated(False)
    self.w.set_title("Screen Saver Screen Locked")        
    self.w.move(0,0)
    #self.w.resize(66,66)

    self.w.set_name("main window")    

    # screenSaverPicture = pictures png or jpeg files shown on the screen 
    self.drawing =  gtk.DrawingArea(...) ## FIXME
    self.drawing.connect('expose-event', self.onExposeEvent)

    self.drawing.connect ('button-press-event', self.callback)
    self.drawing.set_events(gtk.gdk.EXPOSURE_MASK | gtk.gdk.LEAVE_NOTIFY_MASK | gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.POINTER_MOTION_MASK| gtk.gdk.POINTER_MOTION_HINT_MASK )

    self.w.add(self.drawing)

    self.w.show_all()
    self.w.connect("destroy", gtk.main_quit)
    gtk.main()
  def onExposeEvent(self, widget, event):
    x, y, w, h = widget.allocation
    cr = widget.get_window().cairo_create()
    cr.rectangle(0, 0, w, h)
    cr.set_source_rgb(0, 0, 0)
    cr.fill()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top