PyGTK – Dialog Class ”; Previous Next A Dialog widget is normally used as a pop-up window on top of a parent window. The objective of a Dialog is to collect some data from the user and send it to the parent window. Dialog can be modal (where it blocks the parent frame) or modeless (dialog frame can be bypassed). The Dialog widget of PyGTK library is a window split vertically. In its top section, there is a gtk.VBox in which Label or Entry Widgets are packed. The bottom section is called action_area in which one or more buttons are placed. Two areas are separated by gtk.HSeparator. gtk.Dialog class has the following constructor − dlg = gtk.Dialog (Title = None, parent = None, flags = 0, buttons = None) Where, Title − Is the text appearing in the Title bar of the Dialog widget. Parent − Is the reference to the toplevel window from which the dialog pops up. Flag − Defines constants controlling operation of Dialog. The defined constants are − gtk.DIALOG_MODAL If set, the dialog grabs all the keyboard events gtk.DIALOG_DESTROY_WITH_PARENT If set, the dialog is destroyed when its parent is. gtk.DIALOG_NO_SEPARATOR If set, there is no separator bar above the buttons. What is a Button? A Button is a tuple object containing pairs of gtk.Button with a stock ID (or text) and its response IDs. The response ID can be any number or one of the predefined Response ID constants − gtk.RESPONSE_NONE gtk.RESPONSE_REJECT gtk.RESPONSE_ACCEPT gtk.RESPONSE_DELETE_EVENT gtk.RESPONSE_OK gtk.RESPONSE_CANCEL gtk.RESPONSE_CLOSE gtk.RESPONSE_YES gtk.RESPONSE_NO gtk.RESPONSE_APPLY gtk.RESPONSE_HELP The important methods of the gtk.Dialog class are given below − add_button() − Adds a button with the text specified by button_text (or a stock button, if button_text is a stock ID) in action_area. response() − Emits the “response” signal with the value specified in response_id run() − Displays the dialog and returns the response_id when delete_event gets emitted. set_default_response() − Sets the last widget in the dialog”s action area with the specified response_id as the default widget for the dialog. gtk.Dialog widget emits the following signals − Close This is emitted when the dialog is closed. Response This is emitted when an action_area widget is activated (button “clicked”), the dialog receives a delete_event or the application calls the response() method. Two buttons in action_area of Dialog widget use Stock IDs gtk.STOCK.CANCEL and gtk.STOCK_OK. They are associated with response IDs gtk. RESPONSE_REJECT and gtk. RESPONSE_ACCEPT respectively. The Dialog is closed when any button is pressed. The run() methods returns corresponding response ID which may be utilized for further processing. Following code displays a top level gtk.Window with a Button in it. When button is clicked, a Dialog appears with a label and two buttons. Example Observe the following code − import gtk class PyApp(gtk.Window): def __init__(self): super(PyApp, self).__init__() self.set_title(“Dialog Demo”) self.set_default_size(250, 200) fixed = gtk.Fixed() btn = gtk.Button(“Show”) btn.connect(“clicked”,self.show_sialog) fixed.put(btn,100,100) self.add(fixed) self.connect(“destroy”, gtk.main_quit) self.show_all() def show_sialog(self, widget, data=None): dialog = gtk.Dialog(“My dialog”, self, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OK, gtk.RESPONSE_ACCEPT)) label = gtk.Label(“Simple dialog”) dialog.vbox.add(label) label.show() res = dialog.run() print res dialog.destroy() if __name__ == ”__main__”: PyApp() gtk.main() The above code produces the following output − Preconfigured Dialog Widgets PyGTK API has a number of preconfigured Dialog widgets − MessageDialog AboutDialog ColorSelectionDialog FontSelectionDialog FileChooserDialog In order to demonstrate the functioning of the above standard dialog in PyGTK, a menu with a menu item each invoking a dialog when clicked, is put in a gtk.Window in the following program. The Callback functions responding to activate the signal of each menu item is listed. You can also understand the explanation provided for each type of dialog widget. Example Observe the following code − import gtk, pango class PyApp(gtk.Window): def __init__(self): super(PyApp, self).__init__() self.set_title(“Dialog Boxes”) self.set_default_size(250, 200) self.set_position(gtk.WIN_POS_CENTER) mb = gtk.MenuBar() menu1 = gtk.Menu() file = gtk.MenuItem(“_File”) file.set_submenu(menu1) msg = gtk.MenuItem(“MessageDialog”) menu1.append(msg) abt = gtk.MenuItem(“AboutDialog”) menu1.append(abt) colo = gtk.MenuItem(“colorDialog”) menu1.append(colo) font = gtk.MenuItem(“FontSelectionDialog”) menu1.append(font) fl = gtk.MenuItem(“FileChooserDialog”) menu1.append(fl) mb.append(file) vbox = gtk.VBox(False, 2) vbox.pack_start(mb, False, False, 0) self.add(vbox) self.text = gtk.Label(“TutorialsPoint”) vbox.pack_start(self.text, True, True, 0) msg.connect(“activate”,self.on_msgdlg) abt.connect(“activate”,self.on_abtdlg) font.connect(“activate”,self.on_fntdlg) colo.connect(“activate”,self.on_color) fl.connect(“activate”, self.on_file) self.connect(“destroy”, gtk.main_quit) self.show_all() def on_msgdlg(self, widget): #MessageDialog usage code def on_abtdlg(self, widget): #AboutDialog usage code def on_fntdlg(self,widget): #FontSelectionDialog usage code def on_color(self, widget): #ColorChooserDialog usage cde Def on_file(self, widget): #FileChooserDialog usage code if __name__ == ”__main__”: PyApp() gtk.main() The above code will generate the following output − Print Page Previous Next Advertisements ”;
Category: pygtk
PyGTK – Color Selection Dialog ”; Previous Next This is a preconfigured Dialog in PyGTK API which lets the user to select and apply color. It internally embeds a gtk.ColorSelection widget. The gtk.ColorScelection widget presents a colow wheel, and entry boxes for color parameters such as HSV and RGB. New color can be selected by manipulating color wheel or entering color parameters. Its get_current_color is useful for further processing. Following is the prototype of the constructor of the gtk.ColorSelectionDialog class − dlg = gtk.ColorSelectionDialog(title) The current selected color is obtained from the colorsel attribute. The selected color is applied to a widget using modify_fg() or modify_bg() methods. When the ColorDialog menu button is activated, the following callback function is executed − def on_color(self, widget): dlg = gtk.ColorSelectionDialog(“Select color”) col = dlg.run() sel = dlg.colorsel.get_current_color() self.text.modify_fg(gtk.STATE_NORMAL, sel) The chosen color is applied to the text in a label widget on the window − The following is the output − Print Page Previous Next Advertisements ”;
PyGTK – SpinButton Class
PyGTK – SpinButton Class ”; Previous Next The SpinnButton widget, often called the Spinner is a gtk.Entry widget with up and down arrows on its right. A user can type in a numeric value directly in it or increment or decrement using up and down arrows. The gtk.SpinButton class is inherited from the gtk.Entry class. It uses a gtk.Adjustment object with which the range and step of the numeric value in the spinner can be restricted. The SpinButton widget is created using the following constructor − sp = gtk.SpinButton(adj, climb_rate, digits) Here, adj represents the gtk.Adjustment object controlling range, climb_rate is an acceleration factor and the number of decimals specified by digits. The gtk.SpinButton class has the following methods − SpinButton.set_adjustment() − This sets the “adjustment” property. SpinButton.set_digits() − This sets the “digits” property to the value to determine the number of decimal places to be displayed by the spinbutton. SpinButton.set_increments(step, page) − This sets the step value which has increment applied for each left mousebutton press and page value which is increment applied for each middle mousebutton press. SpinButton.set_range() − This sets the minimum and maximum allowable values for spinbutton. SpinButton.set_value() − This sets the spin button to a new value programmatically. SpinButton.update_policy() − The valid values are gtk.UPDATE_ALWAYS and gtk.UPDATE_VALID SpinButton.spin(direction, increment=1) − This increments or decrements Spinner”s value in the specified direction. The following are the predefined direction constants − gtk.SPIN_STEP_FORWARD forward by step_increment gtk.SPIN_STEP_BACKWARD backward by step_increment gtk.SPIN_PAGE_FORWARD forward by step_increment gtk.SPIN_PAGE_BACKWARD backward by step_increment gtk.SPIN_HOME move to minimum value gtk.SPIN_END move to maximum value gtk.SPIN_USER_DEFINED add increment to the value SpinButton.set_wrap() — If wrap is True, the spin button value wraps around to the opposite limit when the upper or lower limit of the range exceeds. The gtk.SpinButton widget emits the following signals − change-value This is emitted when the spinbutton value is changed by keyboard action input This is emitted when the value changes. output This is emitted when the spinbutton display value is changed. Returns True if the handler successfully sets the text and no further processing is required. value-changed This is emitted when any of the settings that change the display of the spinbutton is changed. wrapped This is emitted right after the spinbutton wraps from its maximum to minimum value or vice-versa. Example The following example constructs a simple Date Selector by using three SpinButton widgets. The Day Selector is applied an Adjustment object to restrict value between 1—31. The second selector is for the number of months 1—12. The third selector selects the year range 2000—2020. Observe the code − import gtk class PyApp(gtk.Window): def __init__(self): super(PyApp, self).__init__() self.set_title(“SpinButton Demo”) self.set_size_request(300, 200) self.set_position(gtk.WIN_POS_CENTER) self.set_border_width(20) vbox = gtk.VBox(False, 5) hbox = gtk.HBox(True, 3) lbl1 = gtk.Label(“Date”) hbox.add(lbl1) adj1 = gtk.Adjustment(1.0, 1.0, 31.0, 1.0, 5.0, 0.0) spin1 = gtk.SpinButton(adj1, 0, 0) spin1.set_wrap(True) hbox.add(spin1) lbl2 = gtk.Label(“Month”) hbox.add(lbl2) adj2 = gtk.Adjustment(1.0, 1.0, 12.0, 1.0, 5.0, 0.0) spin2 = gtk.SpinButton(adj2, 0, 0) spin2.set_wrap(True) hbox.add(spin2) lbl3 = gtk.Label(“Year”) hbox.add(lbl3) adj3 = gtk.Adjustment(1.0, 2000.0, 2020.0, 1.0, 5.0, 0.0) spin3 = gtk.SpinButton(adj3, 0, 0) spin3.set_wrap(True) hbox.add(spin3) frame = gtk.Frame() frame.add(hbox) frame.set_label(“Date of Birth”) vbox.add(frame) self.add(vbox) self.connect(“destroy”, gtk.main_quit) self.show_all() PyApp() gtk.main() Upon execution, the above code will produce the following output − Print Page Previous Next Advertisements ”;
PyGTK – Arrow Class
PyGTK – Arrow Class ”; Previous Next The gtk.Arrow object is used to draw simple arrow pointing towards four cardinal directions. This class is inherited from the gtk.Misc class and the object will occupy any space allocated it, for instance, a Label or Button widget. Typically, Arrow object is created using the following constructor − Arr = gtk.Arrow(arrow_type, shadow_type) The predefined arrow_type constants are − gtk.ARROW_UP gtk.ARROW_DOWN gtk.ARROW_LEFT gtk.ARROW_RIGHT The predefined shadow_type constants are listed in the following table − gtk.SHADOW_NONE No outline. gtk.SHADOW_IN The outline is beveled inward. gtk.SHADOW_OUT The outline is beveled outward like a button. gtk.SHADOW_ETCHED_IN The outline itself is an inward bevel, but the frame bevels outward. gtk.SHADOW_ETCHED_OUT The outline is an outward bevel, frame bevels inward. Example In the following example, four Button widgets are added to an Hbox. On top of each button, a gtk.Arrow object pointing UP, DOWN, LEFT and RIGHT respectively is placed. The HBOX container is placed at the bottom of the toplevel window with the help of an Alignment container. Observe the code − import gtk class PyApp(gtk.Window): def __init__(self): super(PyApp, self).__init__() self.set_title(“Arrow Demo”) self.set_size_request(300, 200) self.set_position(gtk.WIN_POS_CENTER) vbox = gtk.VBox(False, 5) hbox = gtk.HBox(True, 3) valign = gtk.Alignment(0, 1, 0, 0) vbox.pack_start(valign) arr1 = gtk.Arrow(gtk.ARROW_UP, gtk.SHADOW_NONE) arr2 = gtk.Arrow(gtk.ARROW_DOWN, gtk.SHADOW_NONE) arr3 = gtk.Arrow(gtk.ARROW_LEFT, gtk.SHADOW_NONE) arr4 = gtk.Arrow(gtk.ARROW_RIGHT, gtk.SHADOW_NONE) btn1 = gtk.Button() btn1.add(arr1) btn2 = gtk.Button() btn2.add(arr2) btn3 = gtk.Button() btn3.add(arr3) btn4 = gtk.Button() btn4.add(arr4) hbox.add(btn1) hbox.add(btn2) hbox.add(btn3) hbox.add(btn4) halign = gtk.Alignment(0.5, 0.5, 0, 0) halign.add(hbox) vbox.pack_start(halign, False, True, 10) self.add(vbox) self.connect(“destroy”, gtk.main_quit) self.show_all() PyApp() gtk.main() The above code will generate the following output − Print Page Previous Next Advertisements ”;
PyGTK – MenuBar,Menu and MenuItem ”; Previous Next A horizontal bar just below the title bar of a toplevel gtk.Window is reserved to display series of menus. It is an object of gtk.MenuBar class in PyGTK API. An object of the gtk.Menu class is added to the menu bar. It is also used to create context menu and popup menu. Each menu may contain one or more gtk.MenuItem widgets. Some of them can be a submenu.and have cascaded MenuItem buttons. The gtk.MenuBar is subclassed from the gtk.MenuShell class. It has a simple default constructor − gtk.MenuBar() To add a menu to MenuBar, the append() method of the MenuBar class is used. In order to construct a menu, create a MenuItem widget with a label which is desired to appear in menu bar and set it as submenu. For example, the following code is used to set up a File menu − menu1 = gtk.Menu() file = gtk.MenuItem(“_File”) file.set_submenu(menu1) Now, one or more widgets of the MenuItem class can be added in the menu. item1 = gtk.MenuItem(“New”) item2 = gtk.MenuItem(“Open”) These MenuItems are added to the Menu widget and the menu object in turn is added to the menu bar. menu1.append(item1) menu1.append(item2) mb.append(menu1) The PyGTK toolkit offers many types of MenuItem widgets. An ImageMenuItem is a menu item with an image associated with it. You can use any of the stock images by using Stock ID parameter or assign any other image by set_image() method. For example, ”New” menu item having image is created in the following way − new = gtk.ImageMenuItem(gtk.STOCK_NEW) menu1.append(new) Similarly, it is also possible to add CheckMenuItem using the following code − chk = gtk.CheckMenuItem(“Checkable”) menu1.append(chk) A group of radio items can also be added using this code − radio1 = gtk.RadioMenuItem(None,”Radio1″) radio2 = gtk.RadioMenuItem(radio1, “Radio2”) menu1.append(radio1) menu1.append(radio2) Sometimes, you may want to add a seperator line between menu items. For that purpose, the SeparatorMenuItem is also available. sep = gtk.SeparatorMenuItem() menu1.append(sep) You can also assign keyboard shortcuts to menu items. PyGTK has accelerators. Start by creating an accelerator group and attach it to the toplevel window. acgroup = gtk.AccelGroup() self.add_accel_group(acgroup) To assign shortcut, use add_accelerator() function with the following prototype − Item1.add_accelerator(signal, group, key, modifier, flags) The following are some of the predefined modifiers − SHIFT_MASK LOCK_MASK CONTROL_MASK BUTTON1_MASK BUTTON1_MASK In order to assign Ctrl+N shortcut to New Menu item, use the following syntax − new = gtk.ImageMenuItem(gtk.STOCK_NEW,acgroup) new.add_accelerator(“activate”, acgroup, ord(”N”), gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE) Example The following example demonstrates the features discussed above − import gtk class PyApp(gtk.Window): def __init__(self): super(PyApp, self).__init__() self.set_title(“Menu Demo”) self.set_default_size(250, 200) self.set_position(gtk.WIN_POS_CENTER) mb = gtk.MenuBar() menu1 = gtk.Menu() file = gtk.MenuItem(“_File”) file.set_submenu(menu1) acgroup = gtk.AccelGroup() self.add_accel_group(acgroup) new = gtk.ImageMenuItem(gtk.STOCK_NEW,acgroup) new.add_accelerator(“activate”, acgroup, ord(”N”), gtk.gdk.CONTROL_MASK, gtk.ACCEL_VISIBLE) menu1.append(new) open = gtk.ImageMenuItem(gtk.STOCK_OPEN) menu1.append(open) chk = gtk.CheckMenuItem(“Checkable”) menu1.append(chk) radio1 = gtk.RadioMenuItem(None,”Radio1″) radio2 = gtk.RadioMenuItem(radio1, “Radio2”) menu1.append(radio1) menu1.append(radio2) sep = gtk.SeparatorMenuItem() menu1.append(sep) exit = gtk.ImageMenuItem(gtk.STOCK_QUIT) menu1.append(exit) menu2 = gtk.Menu() edit = gtk.MenuItem(“_Edit”) edit.set_submenu(menu2) copy = gtk.ImageMenuItem(gtk.STOCK_COPY) menu2.append(copy) cut = gtk.ImageMenuItem(gtk.STOCK_CUT) menu2.append(cut) paste = gtk.ImageMenuItem(gtk.STOCK_PASTE) menu2.append(paste) mb.append(file) mb.append(edit) vbox = gtk.VBox(False, 2) vbox.pack_start(mb, False, False, 0) self.add(vbox) self.connect(“destroy”, gtk.main_quit) self.show_all() if __name__ == ”__main__”: PyApp() gtk.main() The above code will produce the following output − Print Page Previous Next Advertisements ”;
PyGTK – Discussion
Discuss PyGTK ”; Previous Next PyGTK is a set of wrappers written in Python and C for GTK + GUI library. It is part of the GNOME project. It offers comprehensive tools for building desktop applications in Python. This tutorial discusses the basic functionalities of the different widgets found in the toolkit. Print
PyGTK – Clipboard Class
PyGTK – Clipboard Class ”; Previous Next A Clipboard object holds shared data between two processes or two widgets of the same application. The gtk.Clipboard is a high level interface for the gtk.SelectionData class. The following is a prototype of the gtk.Clipboard constructor − gtk.Clipboard(display,selction) Here, the display parameter corresponds to the gtk.gdk.Display object for which the clipboard is to be created or retrieved. By default, it is the standard output device. The selection parameter defaults to CLIPBOARD, an object representing an interned string. PyGTK provides a convenience function to create a clipboard object with defaults. gtk.clipboard.get() gtk.Clipboard class has the following methods − Clipboard.store() − This stores the current clipboard data somewhere so that it will stay around even after the application has quit. Clipboard.clear() − This removes the contents of the clipboard. Clipboard.set_text(text) − This sets the contents of the clipboard to the string. Clipboard.request_text() − This requests the contents of the clipboard as text. When the text is later received, callback will be called with the data specified by user_data. The signature of callback is: def callback(clipboard, text, data) − text will contain the text retrieved from clipboard. As a demonstration of clipboard, the following code uses two TextViews and two buttons on a toplevel gtk.Window. The ”Set” button calls the on_set() function which puts the text from first textView on the clipboard. buf = self.tv1.get_buffer() text = buf.get_text(buf.get_start_iter(), buf.get_end_iter()) self.clipboard = gtk.clipboard_get() self.clipboard.set_text(text) self.clipboard.store() When the second button (”retrieved”) is pressed, the data from clipboard is fetched by the request_text() method − self.clipboard.request_text(self.readclipboard, user_data = None) The content of user_data goes to a callback method readclipboard() which displays it on second textview. def readclipboard(self, clipboard, text, data): buffer = gtk.TextBuffer() buffer.set_text(text) self.tv2.set_buffer(buffer) Example The following is the entire code for clipboard operation − import gtk class PyApp(gtk.Window): def __init__(self): super(PyApp, self).__init__() self.set_title(“Clipboard demo”) self.set_size_request(300,200) self.set_position(gtk.WIN_POS_CENTER) vbox = gtk.VBox(False, 5) self.tv1 = gtk.TextView() vbox.add(self.tv1) self.tv2 = gtk.TextView() vbox.add(self.tv2) hbox = gtk.HBox(True, 3) Set = gtk.Button(“set”) Set.set_size_request(70, 30) retrieve = gtk.Button(“retrieve”) hbox.add(Set) hbox.add(retrieve) halign = gtk.Alignment(1, 0, 0, 0) halign.add(hbox) vbox.pack_start(halign, False, False, 3) self.add(vbox) Set.connect(“clicked”, self.on_set) retrieve.connect(“clicked”, self.on_retrieve) self.connect(“destroy”, gtk.main_quit) self.show_all() def on_set(self, widget): buf = self.tv1.get_buffer() text = buf.get_text(buf.get_start_iter(), buf.get_end_iter()) self.clipboard = gtk.clipboard_get() self.clipboard.set_text(text) self.clipboard.store() def on_retrieve(self, widget): self.clipboard.request_text(self.readclipboard, user_data=None) def readclipboard(self, clipboard, text, data): buffer = gtk.TextBuffer() buffer.set_text(text) self.tv2.set_buffer(buffer) PyApp() gtk.main() The above code will generate the following output − Print Page Previous Next Advertisements ”;
PyGTK – Calendar Class
PyGTK – Calendar Class ”; Previous Next The Calendar widget in PyGTK toolkit displays a simple calendar with one month view at a time. The navigation controls to change month and year are displayed by default. The display options can be suitably configured. The value of month property is between 0 to 11, and that of date property is between 1 to 31. There is a simple constructor to create a gtk.Calendar object − cal = gtk.Calendar() The default display style shows the current month and year as well as names of days. The gtk.Calendar class has the following methods − Calendar.select_month(mm,yy) — This changes the calendar display to the specified mm and yy. Calendar.select_day(dd) — This selects the specified dd on the calendar when it has a value between 1 and 31. If dd is 0 then the current day selection is removed. Calendar.display_options() — This sets the calendar display options to the value specified by flags. The possible display options are a combination of: gtk.CALENDAR_SHOW_HEADING Specifies that the month and year should be displayed. gtk.CALENDAR_SHOW_DAY_NAMES Specifies that three letter day descriptions should be present. gtk.CALENDAR_NO_MONTH_CHANGE Prevents the user from switching months with the calendar. gtk.CALENDAR_SHOW_WEEK_NUMBERS Displays each week numbers of the current year, down the left side of the calendar. gtk.CALENDAR_WEEK_START_MONDAY Starts the calendar week on Monday, instead of the default Sunday. Calendar.get_date() — This retrieves the calendar”s current year, month and selected day numbers as a tuple (year, month, day). The gtk.Calendar widget emits the following signals − day-selected This is emitted when a day is selected either by the user or programmatically. month-changed This is emitted when the calendar month is changed programmatically or by the user. next-month This is emitted when the user clicks the “next-month” navigation control in the calendar header. next-year This is emitted when the user clicks the “next-year” navigation control in the calendar header. prev-month This is emitted when the user clicks the “prev-month” navigation control in the calendar header. prev-year This is emitted when the user clicks the “prev-year” navigation control in the calendar header. In the following example, a gtk.Calendar control and four buttons are placed in the toplevel window. When the ”heading” button is clicked, the Calendar”s display options are set to SHOW_HEADING − def heading(self, widget): self.cal.set_display_options(gtk.CALENDAR_SHOW_HEADING) When the user clicks the ”day name” button, the callback sets display options to SHOW_DAY_NAMES − def dayname(self, widget): self.cal.set_display_options(gtk.CALENDAR_SHOW_DAY_NAMES) Both the display options are enabled when ”both” button is pressed. To begin with, all flags of display options are removed by setting it to 0. self.cal.set_display_options(0) The ”set” button pops up a message box displaying the currently marked date. tp = self.cal.get_date() str1 = str(tp[0]) str2 = str(tp[1]+1) str3 = str(tp[2]) label = gtk.Label(“Date selected:”+str3+”-“+str2+”-“+str1) dialog.vbox.add(label) label.show() Example Observe the following code − import gtk class PyApp(gtk.Window): def __init__(self): super(PyApp, self).__init__() self.set_title(“Calendar Demo”) self.set_size_request(300, 200) self.set_position(gtk.WIN_POS_CENTER) vbox = gtk.VBox(False, 5) self.cal = gtk.Calendar() halign1 = gtk.Alignment(0.5, 0.5, 0, 0) halign1.add(self.cal) self.cal.set_display_options(0) valign = gtk.Alignment(0, 1, 0, 0) vbox.pack_start(halign1) self.btn1 = gtk.Button(“set”) self.btn2 = gtk.Button(“heading”) self.btn3 = gtk.Button(“day name”) self.btn4 = gtk.Button(“Both”) hbox = gtk.HBox(True, 3) hbox.add(self.btn1) hbox.add(self.btn2) hbox.add(self.btn3) hbox.add(self.btn4) halign = gtk.Alignment(0.5, 0.5, 0, 0) halign.add(hbox) vbox.pack_start(halign, False, True, 10) self.add(vbox) self.btn1.connect(“clicked”, self.selectdate) self.btn2.connect(“clicked”, self.heading) self.btn3.connect(“clicked”, self.dayname) self.btn4.connect(“clicked”, self.bothflags) self.connect(“destroy”, gtk.main_quit) self.show_all() def heading(self, widget): self.cal.set_display_options(gtk.CALENDAR_SHOW_HEADING) def dayname(self, widget): self.cal.set_display_options(gtk.CALENDAR_SHOW_DAY_NAMES) def bothflags(self, widget): self.cal.set_display_options(gtk.CALENDAR_SHOW_HEADING|gtk.CALENDAR_SHOW_DAY_NAMES) def selectdate(self, widget): tp = self.cal.get_date() dialog = gtk.Dialog(“My dialog”, self, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_OK, gtk.RESPONSE_ACCEPT)) str1 = str(tp[0]) str2 = str(tp[1]+1) str3 = str(tp[2]) label = gtk.Label(“Date selected:”+str3+”-“+str2+”-“+str1) dialog.vbox.add(label) label.show() res = dialog.run() dialog.destroy() PyApp() gtk.main() The above code will generate the following output − Print Page Previous Next Advertisements ”;
PyGTK – Frame Class
PyGTK – Frame Class ”; Previous Next Frame class is a subclass of the gtk.Bin class. It draws a decorative border around the child widget placed in it. The frame may contain a label whose position may be customized. A gtk.Frame object is constructed with the help of the following constructor − frame = gtk.Frame(label = None) The following are the methods of the gtk.Frame() class − set_label(text) − This sets the label as specified by text. If None, the current label is removed. set_label_widget() − This sets a widget other than gtk.Label as label for frame. set_label_align(x, y) − This sets the alignment of the frame”s label widget and decoration (defaults are 0.0 and 0.5) set_shadow_type() − This sets the frame”s shadow type. The possible values are − gtk.SHADOW_NONE gtk.SHADOW_IN gtk.SHADOW_OUT gtk.SHADOW_ETCHED_IN tk.SHADOW_ETCHED_OUT The following code demonstrates the functioning of the Frame widget. A group of three objects of gtk.RadioButton is placed in an HButtonBox. btn1 = gtk.RadioButton(None,”Degree”) btn2 = gtk.RadioButton(btn1,”P.G.”) btn3 = gtk.RadioButton(btn1,”Doctorate”) hb = gtk.HButtonBox() hb.add(btn1) hb.add(btn2) hb.add(btn3) In order to draw border around the box, it is placed in a Frame widget, and it is added to the toplevel window. frm = gtk.Frame() frm.add(hb) self.add(frm) Example Observe the following code − import gtk class PyApp(gtk.Window): def __init__(self): super(PyApp, self).__init__() self.set_title(“Frame Demo”) self.set_default_size(250, 200) self.set_border_width(5) frm = gtk.Frame() hb = gtk.HButtonBox() btn1 = gtk.RadioButton(None,”Degree”) hb.add(btn1) btn2 = gtk.RadioButton(btn1,”P.G.”) hb.add(btn2) btn3 = gtk.RadioButton(btn1,”Doctorate”) hb.add(btn3) frm.add(hb) frm.set_label(“Qualifications”) self.add(frm) self.connect(“destroy”, gtk.main_quit) self.show_all() if __name__ == ”__main__”: PyApp() gtk.main() The above code will generate the following output − Print Page Previous Next Advertisements ”;
PyGTK – DrawingArea Class
PyGTK – DrawingArea Class ”; Previous Next The DrawingArea widget presents a blank canvas containing a gtk.gdk.Window on which objects such as line, rectangle, arc, etc. can be drawn. PyGTK uses Cairo library for such drawing operations. Cairo is a popular 2D vector graphics library. It is written in C., although, it has bindings in most Languages such as C++, Java, Python, PHP etc. Cairo library can be used to draw on standard output devices in various operating systems. It can also be used to create PDF, SVG and post-script files. In order to perform different drawing operations, we must fetch the device on text of the target output object. In this case, since the drawing is appearing on gtk.DrawingArea widget, the device context of gdk.Window contained inside it is obtained. This class has a cairo-create() method which returns the device context. area = gtk.DrawingArea() dc = area.window.cairo_create() The DrawingArea widget can be connected to the callbacks based on the following signals emitted by it − Realize To take any necessary actions when the widget is instantiated on a particular display. configure_event To take any necessary actions when the widget changes size. expose_event To handle redrawing the contents of the widget when a drawing area first comes on screen, or when it”s covered by another window and then uncovered (exposed). The Mouse and Keyboard events can also be used to invoke callbacks by add_events() method of the gtk.Widget class. Of particular interest is the expose-event signal which is emitted when the DrawingArea canvas first comes up. The different methods for drawing 2D objects, that are defined in the Cairo library are called from this callback connected to the expose-event signal. These methods draw corresponding objects on the Cairo device context. The following are the available drawing methods − dc.rectangle(x,y,w,h) − This draws a rectangle at the specified top left coordinate and having givwn width and height. dc.arc(x,y,r,a1,a2) − This draws a circular arc with given radius and two angles. dc.line(x1, y1, x2, y2) − This draws a line between two pairs of coordinates. dc.line_to(x,y) − This draws a line from the current position to (x,y) dc.show_text(str) − draws string at current cursor position dc.stroke() − draws outline dc.fill() − fills shape with current color dc.set_color_rgb(r,g,b) − sets color to outline and fill with r, g and b values between 0.0 to 1.0 Example The following script draws different shapes and test using Cairo methods. import gtk import math class PyApp(gtk.Window): def __init__(self): super(PyApp, self).__init__() self.set_title(“Basic shapes using Cairo”) self.set_size_request(400, 250) self.set_position(gtk.WIN_POS_CENTER) self.connect(“destroy”, gtk.main_quit) darea = gtk.DrawingArea() darea.connect(“expose-event”, self.expose) self.add(darea) self.show_all() def expose(self, widget, event): cr = widget.window.cairo_create() cr.set_line_width(2) cr.set_source_rgb(0,0,1) cr.rectangle(10,10,100,100) cr.stroke() cr.set_source_rgb(1,0,0) cr.rectangle(10,125,100,100) cr.stroke() cr.set_source_rgb(0,1,0) cr.rectangle(125,10,100,100) cr.fill() cr.set_source_rgb(0.5,0.6,0.7) cr.rectangle(125,125,100,100) cr.fill() cr.arc(300, 50, 50,0, 2*math.pi) cr.set_source_rgb(0.2,0.2,0.2) cr.fill() cr.arc(300, 200, 50, math.pi,0) cr.set_source_rgb(0.1,0.1,0.1) cr.stroke() cr.move_to(50,240) cr.show_text(“Hello PyGTK”) cr.move_to(150,240) cr.line_to(400,240) cr.stroke() PyApp() gtk.main() The above script will generate the following output − Print Page Previous Next Advertisements ”;