运行此代码时,我遇到与您相同的错误。查看下面链接的文档“如果应用程序希望从 EVT_PAINT 事件处理程序中在窗口的客户区上进行绘制,则必须构造 wx.PaintDC。”
http://www.wxpython.org/docs/api/wx.PaintDC-class.html
您会考虑将其更改为客户端 DC 吗? “如果应用程序希望从 EVT_PAINT 事件外部在窗口的客户区上进行绘制,则必须构造 wx.ClientDC”
http://www.wxpython.org/docs/api/wx.ClientDC-class.html
通常我认为您会在绑定到 EVT_PAINT 事件的方法中创建 PaintDC,我检查了一些代码示例,当我希望查找属性或从 Paint 方法外部影响画布时,我通常似乎使用 ClientDC。
为此我会改变:
self.dc = wx.PaintDC(self.panel)
to:
self.dc = wx.ClientDC(self.panel)
这是您的代码的完整修改版本,使用原始 PaintDC 来执行我认为您正在尝试执行的操作。
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.textLog = []
# 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):
dc = wx.PaintDC(self.panel)
dc.SetPen(wx.Pen('black'))
dc.SetBrush(wx.Brush('white'))
dc.Clear()
x, y = 20,20
self.shapeRectangle=dc.DrawRectangle(x, y, 444, 280)
fnt = dc.GetFont()
for i,line in enumerate(self.textLog):
if i%2:
dc.SetTextForeground(wx.RED)
else:
dc.SetTextForeground(wx.BLUE)
dc.SetFont(fnt)
dc.DrawText(line, x, y)
y += dc.GetCharHeight()
# 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.textLog.append(self.sent)
self.panel.Refresh()
self.s.close()
if __name__=="__main__":
app = wx.App(False)
frame = WindowFrame(None, 'ChatClient')
app.MainLoop()