如何在wxPython中链接多个wx.对话框
我想在wxPython(没有其他模块)中制作一个游戏,这样你就可以在游戏开始前在弹出屏幕中输入一些值,然后游戏将被绘制在画布上,画布又被绘制在面板上,面板绑定到主游戏 我用所有新奇的东西制作了游戏屏幕(作品独奏) 我制作了输入屏幕 但我不能把它们联系起来 如何启动游戏,使其打开一个对话框,然后在关闭它时打开另一个对话框,然后打开游戏 我尝试了以下操作,但无法打开画布:如何在wxPython中链接多个wx.对话框,python,wxpython,Python,Wxpython,我想在wxPython(没有其他模块)中制作一个游戏,这样你就可以在游戏开始前在弹出屏幕中输入一些值,然后游戏将被绘制在画布上,画布又被绘制在面板上,面板绑定到主游戏 我用所有新奇的东西制作了游戏屏幕(作品独奏) 我制作了输入屏幕 但我不能把它们联系起来 如何启动游戏,使其打开一个对话框,然后在关闭它时打开另一个对话框,然后打开游戏 我尝试了以下操作,但无法打开画布: # makes a game by showing 2 dialogs # after dialogs have been an
# 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()
在OnInit中,您只需调用对话框并在创建Board实例之前以模块方式显示它们。那么它应该能正常工作 编辑(6-28-12):以下是一些代码:
import wx
########################################################################
class MyDlg(wx.Dialog):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Dialog.__init__(self, None, title="I'm a dialog!")
lbl = wx.StaticText(self, label="Hi from the panel's init!")
btn = wx.Button(self, id=wx.ID_OK, label="Close me")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(lbl, 0, wx.ALL, 5)
sizer.Add(btn, 0, wx.ALL, 5)
self.SetSizer(sizer)
########################################################################
class MyPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
# show a custom dialog
dlg = MyDlg()
dlg.ShowModal()
dlg.Destroy()
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, evt):
pdc = wx.PaintDC(self)
try:
dc = wx.GCDC(pdc)
except:
dc = pdc
rect = wx.Rect(0,0, 100, 100)
for RGB, pos in [((178, 34, 34), ( 50, 90)),
(( 35, 142, 35), (110, 150)),
(( 0, 0, 139), (170, 90))
]:
r, g, b = RGB
penclr = wx.Colour(r, g, b, wx.ALPHA_OPAQUE)
brushclr = wx.Colour(r, g, b, 128) # half transparent
dc.SetPen(wx.Pen(penclr))
dc.SetBrush(wx.Brush(brushclr))
rect.SetPosition(pos)
dc.DrawRoundedRectangleRect(rect, 8)
########################################################################
class MyFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, title="Example frame")
# show a MessageDialog
style = wx.OK|wx.ICON_INFORMATION
dlg = wx.MessageDialog(parent=None,
message="Hello from the frame's init",
caption="Information", style=style)
dlg.ShowModal()
dlg.Destroy()
# create panel
panel = MyPanel(self)
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
frame.Show()
app.MainLoop()
您已经看过了,而且您的示例代码中缺少任何wx.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)。在对话框完成之前,您不想显示帧,因此在\uuuu init\uuuu()
中,调用Hide()
。我还在初始化一个变量,username
,我将在这里存储对话框中的数据
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建议的那样,在制作画布之前先调用对话框<代码>wx。使用showmodel()
启动对话框。通过将self.okButton
的ID设置为常量wx.ID\u OK
,wxPython识别出在单击按钮后应关闭对话框。您还应该知道wx.ID\u 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
我不知道你在问什么。代码中没有
wx.Dialog
对象。你忘了包含一些代码了吗?好的,我知道了,但是你能给我一些示例代码吗?如何先创建一个消息对话框(因为它需要绑定到一个过于正确的框架),然后运行其余的代码?感谢到目前为止的帮助!我确实发了两封邮件,因为我认为我没有正确地表述我的问题。不管怎样,我确实花了很多时间在对话框上,问题是我不知道如何将它们正确地绑定到我的框架面板上。另外,我现在不知道如何开始画布。(我用俄罗斯方块教程作为指导来设置这个框架zetcode.com/wxpython/thetestrisgame)现在我也一直在搞乱底部(如果name='main':等)我想在这里发布我的新代码,但我不知道如何…如果我在我的游戏类的底部使用这个代码,它将运行画布,但不是前面的对话框:if name='main':app=wx.app()frame=Game()board=Speelveld3.Speelveld(frame)board.start()frame.Show()app.MainLoop()@Dieseltjuh使用问题底部的编辑
按钮添加更多信息。此外,还需要缩进整个onInit()
函数。现在它离左边太远了,所以它实际上不是游戏的一部分。请注意,游戏立即开始,但不显示对话框。@Dieseltjuh,因为您是在main
程序中启动棋盘的。去掉那个。您只能从游戏内部调用您的棋盘。而且,您从不调用onInit()
。因此,要么调用onInit()
,要么将其与合并。