矩形不会出现在wxpython上

2024-06-16 10:53:58 发布

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

矩形不会在windows计算机上为我显示。我把这个给了另外一个mac用户,矩形就出现了。我没有收到任何错误,所以我似乎无法解决这个问题。我使用的是python2.7.7。你知道吗

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

Tags: posimportselfsizetitleinitdefdc
1条回答
网友
1楼 · 发布于 2024-06-16 10:53:58

这段代码的问题是,OP想要在面板上绘制,但随后继续告诉PaintDC对象绘制到帧。OnPaint方法应如下所示:

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)

相关问题 更多 >