我正在尝试创建聊天客户端,我获取用户输入并将其显示在我想要绘制的白色矩形上。我尝试在面板上绘制矩形但是我收到此错误
Traceback (most recent call last):
File "C:\Python27\client with gui.py", line 26, in <module>
frame = WindowFrame(None, 'ChatClient')
File "C:\Python27\client with gui.py", line 12, in __init__
self.panel.Bind(wx.EVT_PAINT, self.OnPaint)
AttributeError: 'WindowFrame' object has no attribute 'panel'
import socket
import wx
class WindowFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title = title, size=(500, 400))
panel=wx.Panel(self)
panel.SetBackgroundColour("#E6E6E6")
self.control = wx.TextCtrl(panel, style = wx.TE_MULTILINE, size =(410, 28), pos=(0,329))
sendbutton=wx.Button(panel, label ="Send", pos =(414,325), size=(65,35))
self.panel.Bind(wx.EVT_PAINT, self.OnPaint)
self.Centre()
self.Show()
def OnPaint(self, event):
dc = wx.PaintDC(self)
dc.SetPen(wx.Pen('#d4d4d4'))
dc.SetBrush(wx.Brush('#c56c00'))
dc.DrawRectangle(10, 15, 90, 60)
self.Show(True)
if __name__=="__main__":
app = wx.App(False)
frame = WindowFrame(None, 'ChatClient')
app.MainLoop()
答案 0 :(得分:2)
我相信我已经在OP的other question中回答了这个问题,这与此基本相同。
def OnPaint(self, event):
dc = wx.PaintDC(self.panel) # <<< This was changed
dc.SetPen(wx.Pen('#d4d4d4'))
dc.SetBrush(wx.Brush('#c56c00'))
dc.DrawRectangle(10, 15, 90, 60)
您想要绘制到面板,而不是框架。在OP的代码中,他们告诉wx.PaintDC绘制到self,它指的是框架。我不知道为什么这会在一个操作系统上起作用,除非偶然。它对@ user667648起作用的事实很奇怪。我会把它归档为bug。绘制到面板的正确方法是上面的。
答案 1 :(得分:0)
这一行:
self.panel.Bind(wx.EVT_PAINT, self.OnPaint)
应该是:
panel.Bind(wx.EVT_PAINT, self.OnPaint)
您的类没有属性panel
,但它在init中有一个名为panel
的局部变量。
或者,您可以考虑将面板设为属性:
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("#E6E6E6")
self.control = wx.TextCtrl(self.panel, style = wx.TE_MULTILINE, size =(410, 28), pos=(0,329))
sendbutton=wx.Button(self.panel, label ="Send", pos =(414,325), size=(65,35))
self.panel.Bind(wx.EVT_PAINT, self.OnPaint)
self.Centre()
self.Show()
def OnPaint(self, event):
dc = wx.PaintDC(self)
dc.SetPen(wx.Pen('#d4d4d4'))
dc.SetBrush(wx.Brush('#c56c00'))
dc.DrawRectangle(10, 15, 90, 60)
self.Show(True)
if __name__=="__main__":
app = wx.App(False)
frame = WindowFrame(None, 'ChatClient')
app.MainLoop()
编辑:正如迈克指出绘图程序还有另一个问题。有趣的是我的电脑没有抱怨这个......