Home Home > 2008 > 09 > 23 > Developing with libyui/libzypp & python – part2
Sign up | Login

Developing with libyui/libzypp & python – part2

September 23rd, 2008 by

In part 1 we installed and tested libyui and its python binding. Now let’s take a closer look at its usage.



A start is the “Hello World!” app we already know. The code is:
import yui
#
factory = yui.YUI.widgetFactory()
dialog = factory.createPopupDialog()
#
vbox = factory.createVBox( dialog )
factory.createLabel( vbox, "Hello, World!" )
factory.createPushButton( vbox, "&OK" )
event = dialog.waitForEvent()
dialog.destroy()

In detail and with comments:
a) Initialization
import yui # import the module
#
factory = yui.YUI.widgetFactory() #create a widgetFactory object, mandatory
dialog = factory.createPopupDialog() #create a dialog, mandatory, Popup&Main available

b) Construct GUI
vbox = factory.createVBox( dialog )
# dialog can hold 1 widget, to hold a button and a label we create a vertically stacking box
factory.createLabel( vbox, "Hello, World!" ) # add a Label to the vbox
factory.createPushButton( vbox, "&OK" ) # add a Button to the vbox, will be placed below the label

c) Event handling
(in this simple case without loop)
event = dialog.waitForEvent() # wait for next event
dialog.destroy() # destroy dialog window

Let’s create an more usefull application – e.g. a “repository lister”. It takes a openSUSE BuildService Repository URL as input and lists the available packages. For now, we’ll do the GUI and the rpm magic later in part 3 and 4.
Our GUI should look like this:

Code could look like this: (note: indentations not shown here!)

#coding:utf-8
import yui
#
factory = yui.YUI.widgetFactory()
dialog = factory.createMainDialog()
#
# What do we need ?
# a VBOX ! dialog can hold only 1 widget, a VBox stacks multiple widgets vertically


mainVBox = factory.createVBox(dialog)
#
# a HBox for the upper part

upperHBox = factory.createHBox(mainVBox)
upperHBox.setWeight(1, 10) # 1 = vertical, 10 % size
#
# a Logwindow for the output


lowerLog = factory.createLogView(mainVBox, "Output", 4, 200)
lowerLog.setWeight(1, 90) # 1 = vertical, 90 % size
#
# let's add the input/combo/buttons to #1
#
# Input
myInput = factory.createInputField(upperHBox, "Repository URL")
myInput.setValue("http://software.opensuse.org/repositories/home:/dl9pf:/yuitest/openSUSE_11.0/")
myInput.setStretchable( 0, True ) # stretch the inputbox (0 = horizontally)
#
# ComboBox
myComboBox = factory.createComboBox(upperHBox, "Repository type")
myComboBox.addItem("RPM-MD")
myComboBox.setNotify()
#
# "Go"-Button
myGO = factory.createPushButton(upperHBox, "GO!")
myGO.setStretchable( 1, True ) # strech it vertically
#
# Close
Close = factory.createPushButton(mainVBox, "Close")


The event-code for the while-loop could look like:

# wait for the next event and return it
event = dialog.waitForEvent()
if not event: # no event ? skip !
continue
if event.eventType() == yui.YEvent.CancelEvent: # [x] in the top right corner ;)
dialog.destroy() # bail out
break
#
# check widgets ...
#
if event.widget() == myGO:
# read input again
repoString = myInput.value()
lowerLog.appendLines( str( "Repository URL set to : "+str(repoString) ) )
lowerLog.appendLines("\n")
#todo:
#checkRepo()
#getRepoData()
#listRepo()
#
if event.widget() == Close:
dialog.destroy() # bail out
break

Code can be downloaded here,

Pictures were taken using YDialogSpy by Stefan Hundhammer. Tnx to Klaus Kaempf for the libyui-bindings.

Both comments and pings are currently closed.

Comments are closed.