how do i start a wxPython program with a dialogbox, then pop another dialog and then make a basic canvas [duplicate]

荒凉一梦 提交于 2019-12-25 11:04:27

问题


Possible Duplicate:
How to link multiple wx.Dialogs in wxPython

Hi I want to make a wxPython app which first shows me a messageDialog, then an input dialog (which saves the playerName) and then makes a dc (DrawCanvas).

Can any1 set up this framework for me plz? (I keep mixing up panels with frames and dialogs)


回答1:


I already answered this, but here's my answer again: just put the dialog creation and instantiation right after the Panel.init and before anything else. Here's some example code too:

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()


来源:https://stackoverflow.com/questions/11235944/how-do-i-start-a-wxpython-program-with-a-dialogbox-then-pop-another-dialog-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!