【问题标题】:Works on Fedora but not on Windows, wx.Phyton适用于 Fedora 但不适用于 Windows,wx.Phyton
【发布时间】:2013-01-10 23:29:42
【问题描述】:

嗯,我是 wx 的菜鸟,我 5 天前开始学习它。我正在尝试使用诸如位图按钮之类的卡片制作像记忆一样的游戏,但事件不想绑定在我的卡片上。我在网上搜索了一些人寻求帮助,但他们不知道为什么。我将程序发送给一位在 Linux Fedora 中工作的人,他说它可以工作...... 问题出在 MyDialog 类的函数 Cards 中。我制作了一个类似于这个的测试程序,并将事件绑定在 for 命令中,它可以正常工作。 抱歉,如果此网站某处存在答案,我找不到它...

import random
import wx
global n
global ControlVar
ControlVar = False


class MyDialog(wx.Dialog):
    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self, parent, id, title, size=(200, 150))
        wx.StaticBox(self, -1, 'Card pairs', (5, 5), size=(180, 70))
        wx.StaticText(self, -1, 'Number:    ', (15, 40))

        self.spin = wx.SpinCtrl(self, -1, '1', (65, 40), (60, -1), min=3, max=5)
        self.spin.SetValue(4)
        wx.Button(self, 2, 'Ok', (70, 85), (60, -1))
        self.Bind(wx.EVT_BUTTON, self.OnClose, id=2)
        self.Centre()
        self.ShowModal()
        self.Destroy()

    def OnClose(self, event):
        pair = self.spin.GetValue()
        self.Close()
        return(pair)


class MyMenu(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(1000, 700))
        self.SetMinSize(wx.Size(400, 300))
        self.panel = wx.Panel(self, wx.ID_ANY)
        self.SetIcon(wx.Icon('computer.png', wx.BITMAP_TYPE_ANY))


        bmp = wx.Image('wood.png', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        bitmap = wx.StaticBitmap(self, -1, bmp, (0, 0))

        menubar = wx.MenuBar()
        file = wx.Menu()
        edit = wx.Menu()

        file.Append(101, '&New Game', 'Start a New Game')
        file.AppendSeparator()
        file.Append(105,'&Quit\tEsc', 'Quit the Application')          

        menubar.Append(file, '&File')
        self.SetMenuBar(menubar)
        self.statusbar = self.CreateStatusBar()
        self.Centre()

        self.Bind(wx.EVT_MENU, self.OnNew, id=101)
        self.Bind(wx.EVT_MENU, self.OnQuit, id=105)
        self.panel.Bind(wx.EVT_KEY_DOWN, self.OnKey)

    def OnNew(self, event):
        if ControlVar:
            for i in range(n*2):
                self.dugmad[i].Destroy()
        md = MyDialog(None, -1, 'New Game')
        n = md.OnClose(None)
        self.statusbar.SetStatusText('You Selected {} Pairs.'.format(n))
        self.Cards()

    def OnButton(self, event):
        print('ANYTHING PLEASE!')

## problem ahead!
    def Cards(self):
        image = wx.Image('cveteki.jpg', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        self.dugmad = []
        for i in range(2*n):
            dugme = wx.BitmapButton(self, i, image)
            self.dugmad.append(dugme)
            self.Bind(wx.EVT_BUTTON, self.OnButton, id=i)

        if n == 3:
            self.Draw(2, 3)
        if n == 4:
            self.Draw(2, 4)
        if n == 5:
            self.Draw(2, 5)

    def Draw(self,a, b):            
        gs = wx.GridSizer(a,b,40,40)
        for i in range(n*2):
            gs.Add(self.dugmad[i],0, wx.EXPAND)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(gs, 1, wx.EXPAND | wx.ALL, 40)
        self.SetSizer(vbox)
        self.Layout()
        self.Refresh()
        global ControlVar
        ControlVar=True


    def OnKey(self, event):
        keycode = event.GetKeyCode()
        if keycode == wx.WXK_ESCAPE:
             box = wx.MessageDialog(None, 'Are you sure you want to quit?', 'Quit', wx.YES_NO | wx.ICON_QUESTION)
             if box.ShowModal() == wx.ID_YES:
                self.Close()

    def OnQuit(self, event):
        box = wx.MessageDialog(None, 'Are you sure you want to quit?', 'Quit', wx.YES_NO | wx.ICON_QUESTION)
        if box.ShowModal() == wx.ID_YES:
            self.Destroy()


class MyApp(wx.App):
    def OnInit(self):
        frame = MyMenu(None, -1, 'Memory')
        frame.Show(True)
        return (True)

def main():
    app = MyApp(False)
    app.MainLoop()
main()

【问题讨论】:

    标签: events button wxpython


    【解决方案1】:

    我尝试运行您的代码,但我没有准备好具有这些名称的图像,并且我无法理解您的所有全局变量,并且我收到有关 n 未定义的错误。所以我为你做了一个简单的测试,希望对你有帮助:

    import wx
    app = wx.App()
    
    def onButton(evt):
        print "button pressed!", evt.GetEventObject().GetLabel()
    
    frm = wx.Frame(None)
    
    for i in range(10):
        but = wx.Button(frm, pos=(10, i*20), label="button %s" % i)
        but.Bind(wx.EVT_BUTTON, onButton)
    
    frm.Show()
    app.MainLoop()
    

    如果你真的想要,but.Bind(...) 也可以是 frm.Bind(...)。请注意,我不喜欢 id:我不在乎 wxPython 为按钮分配了什么 id。

    我不确定您的代码有什么问题,因为我无法运行它并且不想用它来调试其他错误。

    再次,我希望这会有所帮助。

    【讨论】:

    • 谢谢。我希望它会有所帮助。全局 n 是一个 sintax 警告,因此它也适用于此。 :)
    • 从您的菜单中选择 File|New 后,我收到了 NameError,而不是 SyntaxWarning。在分配名称之前,您曾提及名称“n”。一开始我不知道 'n' 的用途,所以我停止尝试让您的脚本在我的机器上运行,并进行了我提出的小测试。
    【解决方案2】:

    但是为什么你在创建 MyDialog 之后就销毁它?检查:self.ShowModal() 之后立即有 self.Destroy() 方法调用。

    【讨论】:

    • 好吧,我不记得为什么了。它不再存在了,我删除了那行。但是我的朋友帮助我并找出了问题所在......我放置的每个父母都是 wx.Frame。这就是它不起作用的原因......他还纠正了我的其他错误并解释了错误的原因。
    猜你喜欢
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    相关资源
    最近更新 更多