Wednesday, 29 October 2014

Window Appearance Control


The script shows some techniques on controlling the appearance of a GTK3 window.


  1. Set default window dimensions.
  2. Set the start-up position for a window.
  3. Force a window to be maximized or not to be maximized.
  4. Force a window to take the full screen for display.
  5. Handle window change event.
  6. Read the dimensions of a window.


Source Code


#!/usr/bin/python
# Title    : GTK3 Basic window
# Date     : 16/10/2014
from gi.repository import Gtk

class mainApp():
    def __init__(self):
        # window
        self.win = Gtk.Window()
        self.win.connect('destroy', Gtk.main_quit)
        self.win.connect('size-allocate', self.onChanged)
        self.win.set_title('Window Tricks')
        self.win.set_default_size(320,240)
        self.win.set_position(Gtk.WindowPosition.CENTER)
        mBox = Gtk.VBox(False,2)
        # button
        self.but = Gtk.Button("Click on me")
        self.but.connect("clicked", self.onButClicked)
        mBox.pack_start(self.but,True,True,2)
        # label
        self.lab = Gtk.Label("Message")
        mBox.pack_start(self.lab,False,False,2)
        self.win.add(mBox)
        self.win.show_all()
        self.winstate = 0
    
    def showWinDim(self):
        mSize = self.win.get_size()
        mStr = '[Unmaximized] '
        if self.winstate==1:
            mStr = '[Maximized] '
        elif self.winstate==2:
            mStr = '[Fullscreen] '
        mD = 'W:'+str(mSize[0])+' H:'+str(mSize[1])
        print mD
        self.lab.set_text(mStr+mD)
        
    def onButClicked(self,widget):
        if self.winstate==0:
            self.win.maximize()
            self.winstate=1
        elif self.winstate==1:
            self.win.fullscreen()
            self.winstate=2
        elif self.winstate==2:
            self.win.unfullscreen()
            self.win.unmaximize()
            self.winstate=0

    def onChanged(self,widget,data):
        self.showWinDim()


mApp = mainApp()
Gtk.main()

No comments:

Post a Comment