wxpython won't draw rectangle on panel

情到浓时终转凉″ 提交于 2019-12-12 01:47:10

问题


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

回答1:


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.




回答2:


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...



来源:https://stackoverflow.com/questions/24370218/wxpython-wont-draw-rectangle-on-panel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!