7_window_position.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Simple window - Just a simple window
  4. # Copyright (c) 2016 Jorge Maldonado Ventura
  5. #
  6. # This file is part of Simple window
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. import gi
  19. import sys
  20. gi.require_version('Gtk', '3.0')
  21. from gi.repository import Gtk
  22. class Window(Gtk.Window):
  23. def __init__(self):
  24. Gtk.Window.__init__(self, title='Saludar')
  25. self.set_default_size(400, 300)
  26. self.set_border_width(140)
  27. self.set_position(Gtk.WindowPosition.CENTER)
  28. self.grid = Gtk.Grid()
  29. self.grid.set_column_homogeneous(True)
  30. self.user_name = Gtk.Entry()
  31. self.user_name.set_text('Introduce aquí tu nombre de usuario')
  32. self.passwd = Gtk.Entry()
  33. self.passwd.set_visibility(False)
  34. self.login_button = Gtk.Button('Iniciar sesión')
  35. self.login_button.connect('clicked', self.login)
  36. self.exit_button = Gtk.Button('Salir')
  37. self.exit_button.connect('clicked', self.exit)
  38. self.grid.attach(self.user_name, 0, 0, 2, 1)
  39. self.grid.attach(self.passwd, 0, 1, 2, 1)
  40. self.grid.attach(self.exit_button, 0, 2, 1, 1)
  41. self.grid.attach(self.login_button, 1, 2, 1, 1)
  42. self.add(self.grid)
  43. def login(self, widget):
  44. if self.passwd.get_text() == 'secreta':
  45. print('Bienvenido a tu cuenta, ' + self.user_name.get_text() + '.')
  46. else:
  47. print('Lo siento, la contraseña introducida es incorrecta.')
  48. sys.exit(1)
  49. def exit(self, widget):
  50. print('Hasta otra. Gracias por usar el programa.')
  51. sys.exit(0)
  52. if __name__ == '__main__':
  53. win = Window()
  54. win.connect('delete-event', Gtk.main_quit)
  55. win.show_all()
  56. Gtk.main()