Events 适用于Fedora,但不适用于Windows,wx.Phyton
我对wx很在行,五天前我就开始学习了。我正在尝试用位图按钮之类的卡片制作一个类似于内存的游戏,但事件不想绑定到我的卡片上。我在网上搜索,向一些人寻求帮助,但他们不知道为什么。我把程序发给了一个在Linux Fedora上工作的人,他说它可以工作。。。 问题出在MyDialog类的函数卡中。我制作了一个测试程序,与此类似,并将事件绑定到for命令中,使其正常工作。 对不起,如果这个网站上有答案,我找不到Events 适用于Fedora,但不适用于Windows,wx.Phyton,events,button,wxpython,Events,Button,Wxpython,我对wx很在行,五天前我就开始学习了。我正在尝试用位图按钮之类的卡片制作一个类似于内存的游戏,但事件不想绑定到我的卡片上。我在网上搜索,向一些人寻求帮助,但他们不知道为什么。我把程序发给了一个在Linux Fedora上工作的人,他说它可以工作。。。 问题出在MyDialog类的函数卡中。我制作了一个测试程序,与此类似,并将事件绑定到for命令中,使其正常工作。 对不起,如果这个网站上有答案,我找不到 import random import wx global n global Control
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()
我试图运行你的代码,但我没有准备好这些名称的图像,我无法理解你所有的全局变量,我得到一个关于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
我不确定你的代码出了什么问题,因为我无法运行它,也不想用它调试其他错误
同样,我希望这能有所帮助。但是为什么在MyDialog创建之后就要销毁它呢?检查:self.showmodel()之后立即有self.Destroy()方法调用。谢谢。我希望这会有帮助。全局n是sintax警告,因此它也适用于该警告。:)在从菜单中选择File | New之后,我收到的是一个名称错误,而不是SyntaxWarning。在分配名称“n”之前,您已引用了该名称。我一开始不知道“n”的用途,我停止了让你的脚本在我的机器上运行的尝试,并做了我演示的小测试。我不记得为什么了。它不在了,我把那行删掉了。但是我的朋友帮我解决了问题。。。我放的每一个家长都是wx.Frame。这就是它不起作用的原因。。。他还纠正了我的其他错误,并解释了错误的原因。