I created an easy game with pygtk that starts whit start page (option, new game, ecc) and when i press newgame button i want to change window/frame and replace with the game window. Here is a sample of a code:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Title of Window")
self.set_border_width(20)
layout = Gtk.Box(spacing=10)
self.add(layout)
self.set_default_size(300, 500)
label_start = Gtk.Label("This is start page")
layout.pack_start(label_start, True, True, 0)
new_game_button = Gtk.Button("NEW GAME")
new_game_button.connect("clicked", self.start_new_game)
new_game_button.set_border_width(20)
new_game_button.set_valign(Gtk.Align.CENTER)
layout.pack_start(new_game_button, True, True, 0)
def start_new_game(self, widget):
# here is were i want to go to first page
pass
class FirstPage(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Title of Window")
self.set_border_width(20)
layout = Gtk.Box(spacing=10)
self.add(layout)
self.set_default_size(300, 500)
label_start = Gtk.Label("This is start page")
layout.pack_start(label_start, True, True, 0)
return_button = Gtk.Button("NEW GAME")
return_button.connect("clicked", self.return_start_page)
return_button.set_border_width(20)
return_button.set_valign(Gtk.Align.CENTER)
layout.pack_start(return_button, True, True, 0)
def return_start_page(self, widget):
# here is were i want to manage the event to return to start page
pass
window = MainWindow()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()
"start new game" and "return start page" are methods were i want to manage this event. I would also need to pass additional parameters to New Game. Can i simply add them on init of game window? PS: "game window" is MainWindow in the example and FirstPage represents my "start page" with option menu and others thing.
You should create only a Main window with one Gtk.Box (as container), and the 2 "screens" should be 2 extra Gtk.Boxes in the container box. E.g.:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MainWindow(Gtk.Window):
def __init__(self):
super(MainWindow, self).__init__(title="Title of Window")
self.set_border_width(20)
self.set_default_size(300, 500)
container = Gtk.Box()
self.add(container)
container.show()
self.main = Main(self)
container.add(self.main)
self.first_page = FirstPage(self)
container.add(self.first_page)
class Main(Gtk.Box):
def __init__(self, parent_window):
super().__init__(spacing=10)
self.__parent_window = parent_window
label_start = Gtk.Label("This is start page")
self.pack_start(label_start, True, True, 0)
new_game_button = Gtk.Button("NEW GAME")
new_game_button.connect("clicked", self.start_new_game)
new_game_button.set_border_width(20)
new_game_button.set_valign(Gtk.Align.CENTER)
self.pack_start(new_game_button, True, True, 0)
def start_new_game(self, *args):
self.__parent_window.first_page.show_all()
self.hide()
class FirstPage(Gtk.Box):
def __init__(self, parent_window):
super().__init__(spacing=10)
self.__parent_window = parent_window
label_start = Gtk.Label("This is second page")
self.pack_start(label_start, True, True, 0)
return_button = Gtk.Button("END GAME")
return_button.connect("clicked", self.return_start_page)
return_button.set_border_width(20)
return_button.set_valign(Gtk.Align.CENTER)
self.pack_start(return_button, True, True, 0)
def return_start_page(self, *args):
self.__parent_window.main.show_all()
self.hide()
if __name__ == '__main__':
window = MainWindow()
window.connect("delete-event", Gtk.main_quit)
window.main.start_new_game()
window.show()
Gtk.main()
You can use Gtk.Notebook and switching tabs. E.g.:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MainWindow(Gtk.Window):
def __init__(self):
super(MainWindow, self).__init__(title="Title of Window")
self.set_border_width(20)
self.set_default_size(300, 500)
container = Gtk.Notebook()
container.set_show_tabs(False)
self.add(container)
main = Main(container)
container.append_page(main)
first_page = FirstPage(container)
container.append_page(first_page)
class Main(Gtk.Box):
def __init__(self, parent):
super().__init__(spacing=10)
self.__parent = parent
label_start = Gtk.Label("This is start page")
self.pack_start(label_start, True, True, 0)
new_game_button = Gtk.Button("NEW GAME")
new_game_button.connect("clicked", self.start_new_game)
new_game_button.set_border_width(20)
new_game_button.set_valign(Gtk.Align.CENTER)
self.pack_start(new_game_button, True, True, 0)
def start_new_game(self, widget):
self.__parent.set_current_page(1)
class FirstPage(Gtk.Box):
def __init__(self, parent):
super().__init__(spacing=10)
self.__parent = parent
label_start = Gtk.Label("This is second page")
self.pack_start(label_start, True, True, 0)
return_button = Gtk.Button("END GAME")
return_button.connect("clicked", self.return_start_page)
return_button.set_border_width(20)
return_button.set_valign(Gtk.Align.CENTER)
self.pack_start(return_button, True, True, 0)
def return_start_page(self, widget):
self.__parent.set_current_page(0)
if __name__ == '__main__':
window = MainWindow()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()