【发布时间】: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