【发布时间】:2014-06-24 15:17:13
【问题描述】:
此代码适用于另一台计算机上的其他人,但它似乎不适用于我。我正在使用 python 2.7.7。它对另外两个人效果很好,但它似乎不喜欢我或我的电脑,因为每当我运行它时,它都会给我一条错误消息。大家觉得呢?
Traceback (most recent call last):
File "C:\Python27\python projects\client with gui.py", line 43, in <module>
frame = WindowFrame(None, 'ChatClient')
File "C:\Python27\python projects\client with gui.py", line 10, in __init__
self.dc = wx.PaintDC(self.panel) # <<< This was changed
File "C:\Python27\python projects\lib\site-packages\wx-3.0-msw\wx\_gdi.py", line 5215, in __init__
_gdi_.PaintDC_swiginit(self,_gdi_.new_PaintDC(*args, **kwargs))
PyAssertionError: C++ assertion "Assert failure" failed at ..\..\src\msw\dcclient.cpp(277) in wxPaintDCImpl::wxPaintDCImpl(): wxPaintDCImpl may be created only in EVT_PAINT handler!
import socket
import wx
class WindowFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title = title, size=(500, 400))
self.panel=wx.Panel(self)
self.panel.SetBackgroundColour("#0B3861")
self.control = wx.TextCtrl(self.panel, style = wx.TE_MULTILINE, size =(410, 28), pos=(0,329))
self.dc = wx.PaintDC(self.panel) # <<< This was changed
# Sets up the socket connection
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "127.0.0.1"
port = 6667
self.s.connect((host,port))
# creates send button and binds to event
sendbutton=wx.Button(self.panel, label ="Send", pos =(414,325), size=(65,35))
self.panel.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_BUTTON, self.SendPress, sendbutton )
self.Centre()
self.Show()
#Draws white rectangle
def OnPaint(self, event):
self.dc.SetPen(wx.Pen('black'))
self.dc.SetBrush(wx.Brush('white'))
self.shapeRectangle=self.dc.DrawRectangle(20, 20, 444, 280)
self.Show(True)
# Sets the function of the send button
def SendPress(self, event):
self.sent = self.control.GetValue()
self.s.send(self.sent)
self.control.Clear()
self.dc.DrawText(self.sent, 0, 300 )
self.s.close()
if __name__=="__main__":
app = wx.App(False)
frame = WindowFrame(None, 'ChatClient')
app.MainLoop()
【问题讨论】: