问题描述
我正在尝试制作聊天客户端,在其中获取用户输入并将其显示在我尝试绘制的白色矩形上.我尝试在面板上绘制矩形,但出现此错误
I am trying to make chat client where I get the user input and display it on the white rectangle I am trying to draw. I try drawing the rectangle on the panel but I get this error
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()
推荐答案
我相信我已经在 OP 的 其他问题,和这个基本一样.
I believe I answered this already in the OP's other question, which is basically the same as this one.
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 的事实很奇怪.我会将其归档为错误.绘制到面板的正确方法是以上.
You want to draw to the panel, NOT the frame. In the OP's code, they are telling wx.PaintDC to draw to self, which refers to the frame. I don't know why this would work on one OS except by happenstance. The fact that it worked for @user667648 is weird. I would file that as a bug. The proper way to draw to the panel is the above.
这篇关于wxpython 不会在面板上绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!