我用pygtk创建了一个简单的游戏,它启动了白色启动页面(选项,新游戏,ecc),当我按下newgame按钮时,我想更改窗口/框架并替换为游戏窗口。 以下是代码示例:
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()
"开始新游戏"和"返回开始页面"方法是我想管理这个事件。 我还需要将其他参数传递给New Game。 我可以简单地在游戏窗口的 init 上添加它们吗? PS:"游戏窗口"示例中是MainWindow,FirstPage代表我的"开始页面"选项菜单和其他东西。