2 # -*- coding: utf-8 -*-
4 import os
, sys
, pygtk
, random
8 GLADE_FILE
= 'ui.glade'
15 self
.about_dialog_tree
= gtk
.glade
.XML(GLADE_FILE
, 'about_dialog')
16 self
.about_dialog
= self
.about_dialog_tree
.get_widget('about_dialog')
17 callbacks
= { "on_about_dialog_close":self
._about
_dialog
_hide
,
18 "on_about_dialog_response":self
._about
_dialog
_hide
}
19 self
.about_dialog_tree
.signal_autoconnect(callbacks
)
20 self
.about_dialog
.connect("delete-event", self
.do_not_destroy
)
23 self
.success_dialog_tree
= gtk
.glade
.XML(GLADE_FILE
, 'success_dialog')
24 self
.success_dialog
= self
.success_dialog_tree
.get_widget('success_dialog')
25 callbacks
= { "on_success_dialog_close":self
._success
_dialog
_hide
,
26 "on_success_dialog_response":self
._success
_dialog
_hide
}
27 self
.success_dialog_tree
.signal_autoconnect(callbacks
)
28 self
.success_dialog
.connect("delete-event", self
.do_not_destroy
)
31 self
.ui_tree
= gtk
.glade
.XML(GLADE_FILE
, 'main_window')
32 callbacks
= { "on_main_window_destroy":gtk
.main_quit
,
33 "on_new_menu_entry_activate":self
._new
_game
_callback
,
34 "on_quit_menu_entry_activate":gtk
.main_quit
,
35 "on_about_menu_entry_activate":self
._about
_dialog
_show
,
36 "on_number_button_released":self
._validate
_number
}
37 self
.ui_tree
.signal_autoconnect(callbacks
)
38 self
.ui_tree
.get_widget('main_window').show()
40 self
.spinbutton
= self
.ui_tree
.get_widget('spinbutton')
47 def user_estimation(self
):
48 return int(self
.spinbutton
.get_value())
52 def _new_game_callback(self
, widget
, data
=None): self
._new
_game
()
54 self
.game
['number_to_find'] = random
.randint(0,5000)
55 self
.game
['number_of_tries'] = 0
56 self
._update
_help
_label
('Aucune tentative pour l\'instant')
58 def _validate_number(self
, widget
, data
=None):
59 self
.game
['number_of_tries'] += 1
60 if self
.user_estimation() == self
.game
['number_to_find']:
61 self
.success_dialog
.show()
62 label_string
= 'Yeah, bien joué !'
63 elif self
.user_estimation() > self
.game
['number_to_find']:
64 label_string
= 'Trop grand !'
66 label_string
= 'Trop petit !'
67 label_string
+= "\n(Tentative %d)" % self
.game
['number_of_tries']
68 self
._update
_help
_label
(label_string
)
70 def _update_help_label(self
, string
):
71 self
.ui_tree
.get_widget('help_label').set_text(string
)
75 # allows the popups to be displayed more than once
76 def do_not_destroy(self
, widget
, event
): return True
78 def _success_dialog_hide(self
, widget
, data
=None): self
.success_dialog
.hide()
79 def _about_dialog_show(self
, widget
, data
=None): self
.about_dialog
.show()
80 def _about_dialog_hide(self
, widget
, data
=None): self
.about_dialog
.hide()
82 if __name__
== '__main__':