本文介绍了如何在 wxPython 中链接多个 wx.Dialogs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 wxPython 中制作一个游戏(没有其他模块),并且我想制作它以便您可以在游戏开始前在弹出屏幕中输入一些值,然后游戏将被绘制在画布上绘制在一个面板上,该面板与主游戏绑定.

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 drawn on a canvas which in turn is drawn on a panel, which is bound to the main game.

我用所有花哨的东西制作了游戏画面(单独工作)我制作了输入屏幕但我无法链接它们.

I made the gamescreen with all fancy stuff (works solo)I made the input screensBut I cannot link them.

我如何开始游戏让它打开一个对话框,然后关闭它打开另一个对话框,然后再打开游戏?

How do I start the game so it will open a dialog box, then on the closure of it open another one, and then open the game ?

我尝试了以下操作,但无法打开我的画布:

I tried the following, but it will not open my 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()

推荐答案

在您的 OnInit 中,您只需要在创建 Board 实例之前调用您的对话框并以模态方式显示它们.那么它应该可以正常工作.

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.

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

这篇关于如何在 wxPython 中链接多个 wx.Dialogs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 07:01