【问题标题】:wxpython DrawText returns with errorwxpython DrawText 返回错误
【发布时间】:2014-06-23 23:39:13
【问题描述】:

我正在尝试将在我的控制变量中键入的文本绘制到我在 shapeRectangle 中制作的矩形上。每次我运行程序时,输入一些文本并按回车键,它不会将我输入的内容打印到矩形上,而且当我在这里时,我想我不妨问问你们中是否有人知道如何更改文本的颜色?相反,它返回此错误

Traceback (most recent call last):
  File "C:\Python27\client with gui.py", line 39, in SendPress
    wx.DrawText(self.shapeRectangle, self.sent, 0, 300 )
AttributeError: 'module' object has no attribute 'DrawText'




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("#0B3861")
        self.control = wx.TextCtrl(self.panel, style = wx.TE_MULTILINE, size =(410, 28), pos=(0,329))

        # Sets up the socket connection
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        host = "127.0.0.1"
        port = 6667
        self.s.connect((host,port))

        # creates send button and binds to event
        sendbutton=wx.Button(self.panel, label ="Send", pos =(414,325), size=(65,35))
        self.panel.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_BUTTON, self.SendPress, sendbutton )

        self.Centre()
        self.Show()

        #Draws white rectangle
    def OnPaint(self, event):
        dc = wx.PaintDC(self.panel)  # <<< This was changed
        dc.SetPen(wx.Pen('black'))
        dc.SetBrush(wx.Brush('white'))
        self.shapeRectangle=dc.DrawRectangle(20, 20, 444, 280)
        self.Show(True)


        # Sets the function of the send button
    def SendPress(self, event):
        self.sent = self.control.GetValue()
        self.s.send(self.sent)
        self.control.Clear()
        wx.DrawText(self.shapeRectangle, self.sent, 0, 300 )
        self.s.close()

if __name__=="__main__": 
    app = wx.App(False)
    frame = WindowFrame(None, 'ChatClient')
    app.MainLoop()

【问题讨论】:

  • 应该是dc.DrawText()
  • @ohad dc.DrawText 不起作用
  • 如果在网站上这么说会很有用
  • 是的,我刚试过,但没有:(
  • @PadraicCunningham 为什么不呢?因为dc 不是WindowFrame 中的全局对象?我注意到了。如果我错了,请纠正我。

标签: python python-2.7 wxpython


【解决方案1】:

这段代码应该可以工作。

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("#0B3861")
        self.control = wx.TextCtrl(self.panel, style = wx.TE_MULTILINE, size =(410, 28), pos=(0,329))
        self.dc = wx.PaintDC(self.panel)  # <<< This was changed
        # Sets up the socket connection
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        host = "127.0.0.1"
        port = 6667
        self.s.connect((host,port))

        # creates send button and binds to event
        sendbutton=wx.Button(self.panel, label ="Send", pos =(414,325), size=(65,35))
        self.panel.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_BUTTON, self.SendPress, sendbutton )

        self.Centre()
        self.Show()

        #Draws white rectangle
    def OnPaint(self, event):
        self.dc.SetPen(wx.Pen('black'))
        self.dc.SetBrush(wx.Brush('white'))
        self.shapeRectangle=self.dc.DrawRectangle(20, 20, 444, 280)
        self.Show(True)


        # Sets the function of the send button
    def SendPress(self, event):
        self.sent = self.control.GetValue()
        self.s.send(self.sent)
        self.control.Clear()
        self.dc.DrawText(self.sent, 0, 300 )
        self.s.close()

if __name__=="__main__":
    app = wx.App(False)
    frame = WindowFrame(None, 'ChatClient')
    app.MainLoop()

【讨论】:

  • wx.DC.DrawText 获取 4 个参数:(self, text, x, y)self.shapeRectangle 不是必需的,它不会起作用。
  • @ohad,我使用了定义 DrawText 的 OP 代码,我认为它具有正确的参数。问题是访问 DrawText 方法。你遇到了什么错误?
  • @Ohad,不用担心,我认为 OP 实际上使用了正确的参数。
  • @Ohad Traceback(最近一次调用最后):文件“C:\Python27\client with gui.py”,第 40 行,在 SendPress self.dc.DrawText(self.shapeRectangle, self.sent , 0, 300 ) 文件“C:\Python27\lib\site-packages\wx-3.0-msw\wx_gdi.py”,第 3823 行,在 DrawText 返回 gdi.DC_DrawText(*args, * *kwargs) TypeError: DC_DrawText() 最多接受 4 个参数(给定 5 个)
  • @Ohad Traceback(最近一次调用最后):文件“C:\Python27\client with gui.py”,第 44 行,在 frame = WindowFrame(None, 'ChatClient') File "C:\Python27\client with gui.py", line 11, in init self.dc = wx.PaintDC(self.panel) # init gdi.PaintDC_swiginit(self,gdi.new_PaintDC(*args, **kwargs)) PyAssertionError: C++ assertion "Assert failure" failed at ..\..\src\msw\dcclient.cpp(277) in wxPaintDCImpl::wxPaintDCImpl(): wxPaintDCImpl 可能被创建仅在 EVT_PAINT 手中
猜你喜欢
  • 2021-09-06
  • 1970-01-01
  • 1970-01-01
  • 2015-08-03
  • 1970-01-01
  • 2011-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多