如何链接在多个wxPython的wx.Dialogs(How to link multiple wx

2019-07-30 03:31发布

我想在wxPython的(没有其他模块)一个游戏,我希望把它,这样就可以在弹出的界面中输入一些值,在比赛开始前,然后游戏会而这又是画在画布上绘制一个面板,其被结合至主游戏。

我做了gamescreen与所有喜欢的东西(作品独奏)我所做的输入屏幕,但我不能将它们链接。

我该如何开始游戏所以它会打开一个对话框,然后在它打开另一个关闭,然后打开游戏?

我尝试以下,但它不会打开我的画布:

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

Answer 1:

你双贴 ,以及缺乏任何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 这里 )。 你不想显示你的框架后直到对话完成,所以在您的__init__()调用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)

现在,为你的对话。 像迈克·德里斯科尔建议,你打电话给你的对话才作出你的画布。 wx.Dialogs使用推出ShowModal() 通过对ID设置self.okButton到恒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


Answer 2:

在你的OnInit你只需要打电话给你的对话和模态展现在他们面前你创建你局实例。 然后,它应该正常工作。

编辑(12年6月28日):下面是一些代码:

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


文章来源: How to link multiple wx.Dialogs in wxPython