鱼C论坛

 找回密码
 立即注册
查看: 797|回复: 1

[已解决]wxPython问题求助

[复制链接]
发表于 2020-7-31 21:28:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
想实现一个画图板。运行下面的代码出现了一些问题:
Traceback (most recent call last):
  File "E:\Python\installation path\Pythondebug\SketchWindow.py", line 38, in OnLeftDown
    self.pos = event.GetPositionTuple() #5 得到鼠标的位置
AttributeError: 'MouseEvent' object has no attribute 'GetPositionTuple'

Traceback (most recent call last):
  File "E:\Python\installation path\Pythondebug\SketchWindow.py", line 49, in OnMotion
    dc = wx.BufferedDc(wx.ClientDC(self),self.buffer) #9 创建另一个缓存的上下文
AttributeError: module 'wx' has no attribute 'BufferedDc'

Traceback (most recent call last):
  File "E:\Python\installation path\Pythondebug\SketchWindow.py", line 46, in OnLeftUp
    self.ReleaseMouse() #7 释放鼠标
wx._core.wxAssertionError: C++ assertion ""!wxMouseCapture::stack.empty()"" failed at ..\..\src\common\wincmn.cpp(3370) in wxWindowBase::ReleaseMouse(): Releasing mouse capture but capture stack empty?
  1. import wx
  2. class SketchWindow(wx.Window):
  3.     def __init__(self,parent,ID):
  4.         wx.Window.__init__(self,parent,ID)
  5.         self.SetBackgroundColour('White')
  6.         self.color = 'Black'
  7.         self.thickness = 1
  8.         self.pen = wx.Pen(self.color,self.thickness,wx.SOLID) #1 创建一个wx.Pen对象
  9.         self.lines = []
  10.         self.curLine = []
  11.         self.pos = (0,0)
  12.         self.InitBuffer()
  13.     #2 连接事件
  14.         self.Bind(wx.EVT_LEFT_DOWN,self.OnLeftDown)
  15.         self.Bind(wx.EVT_LEFT_UP,self.OnLeftUp)
  16.         self.Bind(wx.EVT_MOTION,self.OnMotion)
  17.         self.Bind(wx.EVT_SIZE,self.OnSize)
  18.         self.Bind(wx.EVT_IDLE,self.OnIdle)
  19.         self.Bind(wx.EVT_PAINT,self.OnPaint)
  20.     def InitBuffer(self):
  21.         size = self.GetClientSize()
  22.     #3 创建一个缓存的设备上下文
  23.         self.buffer = wx.Bitmap(size.width,size.height)
  24.         dc = wx.BufferedDC(None,self.buffer)
  25.     #4 使用设备上下文
  26.         dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
  27.         dc.Clear()
  28.         self.DrawLines(dc)
  29.         self.reInitBuffer = False
  30.     def GetLinesData(self):
  31.         return self.lines[:]
  32.     def SetLinesData(self,lines):
  33.         self.lines = lines[:]
  34.         self.InitBuffer()
  35.         self.Refresh()
  36.     def OnLeftDown(self,event):
  37.         self.curLine = []
  38.         self.pos = event.GetPositionTuple() #5 得到鼠标的位置
  39.         self.CaptureMouse() #6 捕获鼠标
  40.     def OnLeftUp(self,event):   
  41.         if self.HasCapture():
  42.             self.lines.append((self.color,
  43.                                self.thickness,
  44.                                self.curLine))
  45.         self.curLine = []
  46.         self.ReleaseMouse() #7 释放鼠标
  47.     def OnMotion(self,event):
  48.         if event.Dragging() and event.LeftIsDown(): #8确定是否在拖动
  49.             dc = wx.BufferedDc(wx.ClientDC(self),self.buffer) #9 创建另一个缓存的上下文
  50.             self.drawMotion(dc,event)
  51.         event.Skip()
  52.     #10 绘画到设备上下文
  53.     def drawMotion(self,dc,event):
  54.         dc.SetPen(self.pen)
  55.         newPos = event.GetPositionTuple()
  56.         coords = self.pos+newPos
  57.         self.curLine.append(coords)
  58.         dc.DrawLine(*coords)
  59.         self.pos = newPos
  60.     def OnSize(self,event):
  61.         self.reInitBuffer = True #11 处理一个resize事件
  62.     def OnIdle(self,event): #12 空闲时的处理
  63.         if self.reInitBuffer:
  64.             self.InitBuffer()
  65.             self.Refresh(False)
  66.     def OnPaint(self,event):
  67.         dc = wx.BufferedPaintDC(self,self.buffer) #13 处理一个painta(描绘请求)事件
  68.     #14 绘制所有的线条
  69.     def DrawLines(self,dc):
  70.         for colour,thickness,line in self.lines:
  71.             pen = wx.Pen(colour,thickness,wx.SOLID)
  72.             dc.SetPen(pen)
  73.             for coords in line:
  74.                 dc.DrawLine(*coords)
  75.     def SetColor(self,color):
  76.         self.color = color
  77.         self.pen = wx.Pen(self.color,self.thickness,wx.SOLID)
  78.     def SetThickness(self,num):
  79.         self.thickness = num
  80.         self.pen = wx.Pen(self.color,self.thickness,wx.SOLID)
  81. class SketchFrame(wx.Frame):
  82.     def __init__(self,parent):
  83.         wx.Frame.__init__(self,parent,-1,'Sketch Frame',
  84.                           size=(800,600))
  85.         self.sketch =SketchWindow(self,-1)
  86. if __name__ == '__main__':
  87.     app = wx.App()
  88.     frame = SketchFrame(None)
  89.     frame.Show(True)
  90.     app.MainLoop()
  91.         
  92.    
  93.         
复制代码

最佳答案
2020-7-31 22:54:51
你这代码是网上找的吧。网上的代码大多都过时了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-31 22:54:51 | 显示全部楼层    本楼为最佳答案   
你这代码是网上找的吧。网上的代码大多都过时了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-24 12:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表