How to link multiple wx.Dialogs in wxPython

前端 未结 2 394
轻奢々
轻奢々 2021-01-27 00:09

I want to make a game in wxPython (no other modules) and I want to make it so that you can enter some values in popup screens before the game starts, and then the game will be d

相关标签:
2条回答
  • 2021-01-27 00:23

    You've double posted, and the lack of any wx.Dialog in your sample code suggests to me that you haven't even looked at a tutorial yet, but I will give you the benefit of the doubt.

    First, if you want to return information from a dialog, the easiest way is to define a custom dialog. Define a new class that inherits from wx.Dialog and then set it up just like you would a normal panel or a frame. It seems to me that you will need two of these. They'll look something like this:

    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
    

    Now, for the logic you want. Pretty much every object in wxPython that you actually see has the functions Show() and Hide() (API here). You don't want to show your frame until AFTER the dialogs are finished, so in your __init__(), call Hide(). I'm also initializing a variable, username, which is where I will store the data from my dialog.

    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)
    

    Now, for your dialogs. Like Mike Driscoll suggested, you call your dialogs BEFORE making your canvas. wx.Dialogs are launched using ShowModal(). By setting the ID of self.okButton to the constant wx.ID_OK, wxPython recognizes that the dialog should be closed after the button in clicked. You should also be aware of 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
    
    0 讨论(0)
  • 2021-01-27 00:38

    In your OnInit you just need to call your dialogs and show them modally BEFORE you create your Board instance. Then it should work correctly.

    EDIT (6-28-12): Here's some code:

    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()
    
    0 讨论(0)
提交回复
热议问题