蟒蛇.需要帮助解决混淆错误 [英] wxpython. Need assistance with confusing error

查看:29
本文介绍了蟒蛇.需要帮助解决混淆错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码适用于另一台计算机上的其他人,但似乎不适用于我.我正在使用 python 2.7.7.它对另外两个人运行良好,但它似乎不喜欢我或我的电脑,因为每当我运行它时,它都会给我一条错误消息.大家怎么看?

This code works for someone else on another computer but it won't seem to work for me. I am using python 2.7.7. It worked well for two other people but it just seems to not like me or my computer because whenever I run it, it gives me an error message. What do you guys think?

Traceback (most recent call last):
  File "C:\Python27\python projects\client with gui.py", line 43, in <module>
    frame = WindowFrame(None, 'ChatClient')
  File "C:\Python27\python projects\client with gui.py", line 10, in __init__
    self.dc = wx.PaintDC(self.panel)  # <<< This was changed
  File "C:\Python27\python projects\lib\site-packages\wx-3.0-msw\wx\_gdi.py", line 5215, in __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 may be created only in EVT_PAINT handler!





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

推荐答案

例子有不少问题:

  • 绝对定位而不是sizer
  • 绘制时没有自动换行(自己渲染文本通常是个坏主意)
  • 区分发送/接收的奇怪方法 (i%2 ???)

在这种情况下,使用适当的控件来显示彩色文本会更合适.插入您认为合适的发送/接收机制

In this case it would be much more appropriate to use a proper control for having coloured text. Plug in your send/receive mechanism as you see fit

import wx
from wx.stc import StyledTextCtrl

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.stc = StyledTextCtrl(self.panel, -1)
        self.control = wx.TextCtrl(self.panel, style = wx.TE_MULTILINE)
        sendbutton=wx.Button(self.panel, label ="Send")
        recvbutton=wx.Button(self.panel, label ="Receive")

        sendbutton.Bind(wx.EVT_BUTTON, self.SendPress)
        recvbutton.Bind(wx.EVT_BUTTON, self.RecvPress)

        szmain = wx.BoxSizer(wx.VERTICAL)
        szmain.Add(self.stc, 1, wx.EXPAND|wx.ALL, 4)
        szsub = wx.BoxSizer(wx.HORIZONTAL)
        szsub.Add(self.control, 1, wx.EXPAND|wx.ALL, 4)
        szsub.Add(sendbutton, 0, wx.EXPAND|wx.ALL, 4)
        szsub.Add(recvbutton, 0, wx.EXPAND|wx.ALL, 4)
        szmain.Add(szsub, 0, wx.EXPAND|wx.ALL, 0)

        self.panel.SetSizer(szmain)
        self.Centre()
        self.Show()

        # define styles for stc, 0 red fore color, 1 blue fore color
        self.stc.StyleSetSpec(0, "fore:#FF0000")
        self.stc.StyleSetSpec(1, "fore:#0000FF")
        self.stc.SetWrapMode(True) #autowrap

    def add_text(self, text, style):
        curpos = self.stc.GetCurrentPos()
        self.stc.AddText(text)
        self.stc.AddText('\r\n')
        newpos = self.stc.GetCurrentPos()
        delta = newpos-curpos

        # consult the Scintilla doc (stc is based on it) to understand how styling works
        # http://www.scintilla.org/ScintillaDoc.html#Styling
        self.stc.StartStyling(curpos, 31) # mask 31 allows setting of styles
        self.stc.SetStyling(delta, style)
        self.stc.ScrollToEnd() # keep last line visible

        # Sets the function of the send button
    def SendPress(self, event):
        self.add_text(self._get_text(), 0)

        # Sets the function of the recv button
    def RecvPress(self, event):
        self.add_text(self._get_text(), 1)

    def _get_text(self):
        text = self.control.GetValue()
        self.control.Clear()
        return text


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

查看 wxPython 演示如何使用 StyledTextControl(基于 Scintilla).最后但并非最不重要的一点是它让我学会了 STC.

Look in the wxPython demo how to use the StyledTextControl (based on Scintilla). And last but not least it made me learn STC.

这篇关于蟒蛇.需要帮助解决混淆错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆