wxpython不会在pan上绘制矩形

2024-04-23 19:30:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使聊天客户端,我得到用户的输入,并显示在我试图绘制的白色矩形。我试着在面板上画矩形,但是我得到了这个错误

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

Tags: selfclientsizetitleinitwithguidc
2条回答

这条线:

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

应该是:

^{pr2}$

您的类没有属性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()

enter image description here

编辑:正如迈克指出的,绘图程序还有另一个问题。有趣的是我的电脑没有抱怨。。。在

我相信我已经在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)

你想画到面板上,而不是框架上。在操作代码里,他们在说wx.PaintDC公司画到自我,指的是框架。我不知道为什么这会在一个操作系统上运行,除非是偶然的。它为@user667648工作的事实很奇怪。我会把它当作窃听器归档。绘制到面板的正确方法是上面的。在

相关问题 更多 >