简体   繁体   中英

wxpython won't draw rectangle on panel

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()

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)

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.

This line:

    self.panel.Bind(wx.EVT_PAINT, self.OnPaint)

Should be:

    panel.Bind(wx.EVT_PAINT, self.OnPaint)

Your class has no attribute panel , but it does have a local variable in init called panel .

Alternatively, you could consider making panel an attribute:

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()

在此处输入图片说明

EDIT: As Mike points out there is another issue with the drawing routine. Interesting how my computer doesn't complain about this...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM