我想用 wxPython 制作一个游戏(没有其他模块),并且我想制作它,以便您可以在游戏开始之前在弹出屏幕中输入一些值,然后游戏将绘制在 Canvas 上,然后依次绘制在 Canvas 上绘制在与主游戏绑定(bind)的面板上。
我用所有奇特的东西制作了游戏屏幕(单独工作) 我制作了输入屏幕 但我无法链接它们。
如何启动游戏,使其打开一个对话框,然后在关闭时打开另一个对话框,然后打开游戏?
我尝试了以下操作,但它不会打开我的 Canvas :
# makes a game by showing 2 dialogs
# after dialogs have been answered, starts the game by drawing the canvas.
# imports
import wx
import Speelveld3
# globals
SCRWIDTH = 950
SCRHEIGHT = 700
# dialogbox class
class MyDialog1(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent)
self.username = wx.TextCtrl(self)
self.okButton = wx.Button(self, wx.ID_OK, "OK")
class MyDialog2(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent)
self.canvasWidth = wx.TextCtrl(self)
self.okButton = wx.Button(self, wx.ID_OK, "OK")
# main class
class Game(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, title='My game', size=(SCRWIDTH, SCRHEIGHT))
self.username = ""
self.canvasWidth = 10
# hide the frame for now
self.Hide()
def OnInit(self):
#Make your dialogs
dlg1 = MyDialog1(self)
#if the user pressed "OK" (i.e. NOT "Cancel" or any other button you might add)
if dlg1.ShowModal() == wx.ID_OK:
#get the username from the dialog
self.username = dlg1.username.GetValue()
#clean up the dialog (AFTER you get the username)
dlg1.Destroy()
dlg2 = MyDialog2(self)
#if the user pressed "OK" (i.e. NOT "Cancel" or any other button you might add)
if dlg2.ShowModal() == wx.ID_OK:
#get the username from the dialog
self.canvasWidth = dlg2.canvasWidth.GetValue()
#clean up the dialog (AFTER you get the username)
dlg2.Destroy()
# Now that you have your settings, Make the gameboard
# THIS PART IS STILL BROKEN!
# I can paste the whole board class (structure of it is taken from the tetris tutorial)
# but that seems a bit much tbh...
self.gameBoard = Board.Board(self)
self.gameBoard = SetFocus()
self.gameBoard.start()
self.Centre()
self.Show(True) #show the frame
if __name__ == '__main__':
# how can I start the game here?
app = wx.App()
frame = Game()
board = Speelveld3.Speelveld(frame)
board.start()
frame.Show()
app.MainLoop()
最佳答案
你已经double posted ,并且您的示例代码中缺少任何 wx.Dialog
,这表明您甚至还没有看过教程,但我会给您带来好处。
首先,如果你想从对话框返回信息,最简单的方法是定义一个 custom dialog 。定义一个继承自wx.Dialog
的新类,然后像普通面板或框架一样设置它。在我看来,你需要其中两个。它们看起来像这样:
class MyDialog1(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent)
self.username = wx.TextCtrl(self) #this is where users will enter their username
self.okButton = wx.Button(self, wx.ID_OK, "OK") #Note that I'm using wx.ID_OK. This is important
现在,为您想要的逻辑。几乎您实际看到的 wxPython 中的每个对象都具有函数 Show()
和 Hide()
(API here )。在对话框完成之前,您不想显示框架,因此在 __init__()
中,调用 Hide()
。我还初始化了一个变量用户名
,我将在其中存储对话框中的数据。
class Game(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(SCRWIDTH, SCRHEIGHT))
self.username = ""
self.Hide() #don't show the frame just yet
#self.Hide() is the exact same as self.Show(False)
现在,开始对话。就像迈克·德里斯科尔(Mike Driscoll)建议的那样,在制作 Canvas 之前调用对话框。 wx.Dialogs
使用 ShowModal()
启动。通过将self.okButton
的ID设置为常量wx.ID_OK
,wxPython识别出单击按钮后应该关闭对话框。您还应该注意 wx.ID_CANCEL
。
def OnInit(self):
#Make your dialogs
dlg1 = MyDialog1(self)
if dlg1.ShowModal() == wx.ID_OK:
#if the user pressed "OK" (i.e. NOT "Cancel" or any other button you might add)
self.username = dlg1.username.GetValue() #get the username from the dialog
dlg1.Destroy() #clean up the dialog (AFTER you get the username)
#do this again for your second dialog
#Now that you have your settings, Make the gameboard
self.gameBoard = Board.Board(self)
self.gameBoard = SetFocus()
self.gameBoard.start()
self.Centre()
self.Show(True) #show the frame
关于python - 如何在wxPython中链接多个wx.Dialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11215632/