AttributeError: ‘MouseEvent‘ object has no attribute ‘GetPositionTuple‘问题解决
最近在学wxpython的绘画程序,遇到以下问题:
AttributeError: 'MouseEvent' object has no attribute 'GetPositionTuple'
网上搜了一下答案:
wxpython 4 不支持wx.MouseEvent.GetPositionTuple()
所以在获取鼠标位置时,如果要变成元组,就需要写成:
tuple(wx.MouseEvent.GetPosition())
所以绘画程序的第一个代码,需要改成一下:
self.pos = event.GetPositionTuple()
改成:
self.pos = tuple(event.GetPosition())newPos = event.GetPositionTuple()
改成:
newPos = tuple(event.GetPosition())
效果:
如对您有帮助,点赞关注收藏♥,谢谢!
完整代码如下:
import wxclass SketchWindow(wx.Window):def __init__(self, parent, ID):wx.Window.__init__(self, parent, ID)self.SetBackgroundColour("White")self.color = "Black"self.thickness = 1self.pen = wx.Pen(self.color, self.thickness, wx.SOLID) # 1 创建一个wx.Pen对象self.lines = []self.curLine = []self.pos = (0, 0)self.InitBuffer()# 2 连接事件self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)self.Bind(wx.EVT_MOTION, self.OnMotion)self.Bind(wx.EVT_SIZE, self.OnSize)self.Bind(wx.EVT_IDLE, self.OnIdle)self.Bind(wx.EVT_PAINT, self.OnPaint)def InitBuffer(self):size = self.GetClientSize()# 3 创建一个缓存的设备上下文self.buffer = wx.EmptyBitmap(size.width, size.height)dc = wx.BufferedDC(None, self.buffer)# 4 使用设备上下文dc.SetBackground(wx.Brush(self.GetBackgroundColour()))dc.Clear()self.DrawLines(dc)self.reInitBuffer = Falsedef GetLinesData(self):return self.lines[:]def SetLinesData(self, lines):self.lines = lines[:]self.InitBuffer()self.Refresh()def OnLeftDown(self, event):self.curLine = []self.pos = tuple(event.GetPosition()) # 5 得到鼠标的位置self.CaptureMouse() # 6 捕获鼠标def OnLeftUp(self, event):if self.HasCapture():self.lines.append((self.color,self.thickness,self.curLine))self.curLine = []self.ReleaseMouse() # 7 释放鼠标def OnMotion(self, event):if event.Dragging() and event.LeftIsDown(): # 8 确定是否在拖动dc = wx.BufferedDC(wx.ClientDC(self), self.buffer) # 9 创建另一个缓存的上下文self.drawMotion(dc, event)event.Skip()# 10 绘画到设备上下文def drawMotion(self, dc, event):dc.SetPen(self.pen)newPos = tuple(event.GetPosition())coords = self.pos + newPosself.curLine.append(coords)dc.DrawLine(*coords)self.pos = newPosdef OnSize(self, event):self.reInitBuffer = True # 11 处理一个resize事件def OnIdle(self, event): # 12 空闲时的处理if self.reInitBuffer:self.InitBuffer()self.Refresh(False)def OnPaint(self, event):dc = wx.BufferedPaintDC(self, self.buffer) # 13 处理一个paint(描绘)请求# 14 绘制所有的线条def DrawLines(self, dc):for colour, thickness, line in self.lines:pen = wx.Pen(colour, thickness, wx.SOLID)dc.SetPen(pen)for coords in line:dc.DrawLine(*coords)def SetColor(self, color):self.color = colorself.pen = wx.Pen(self.color, self.thickness, wx.SOLID)def SetThickness(self, num):self.thickness = numself.pen = wx.Pen(self.color, self.thickness, wx.SOLID)class SketchFrame(wx.Frame):def __init__(self, parent):wx.Frame.__init__(self, parent, -1, "Sketch Frame",size=(800, 600))self.sketch = SketchWindow(self, -1)if __name__ == '__main__':app = wx.PySimpleApp()frame = SketchFrame(None)frame.Show(True)app.MainLoop()
AttributeError: ‘MouseEvent‘ object has no attribute ‘GetPositionTuple‘问题解决相关推荐
- FlyAI出现AttributeError: 'NoneType' object has no attribute 'input_x'问题解决
FlyAI是一个AI竞赛平台,不过多介绍,见官网:https://www.flyai.com/ 笔者系统:Ubuntu 16.04 (安装了ROS kinetic) 使用以下命令进行测试时 ./fl ...
- AttributeError: ‘module’ object has no attribute’xxx 问题解决
报错:AttributeError: 'module' object has no attribute'xxx 原来是,你创建的py脚本名字不能与python预留字,模块名等相同.比如包叫abcsat ...
- Scrapy Crawl 运行出错 AttributeError: 'xxxSpider' object has no attribute '_rules' 的问题解决...
按照官方的文档写的demo,只是多了个init函数,最终执行时提示没有_rules这个属性的错误日志如下: ......File "C:\ProgramData\Anaconda3\lib\ ...
- 问题解决:AttributeError: ResultSet object has no attribute ‘get‘.
问题: list_links_docs = []soup = get_link_decode(URL)for link in range(10):link = soup.find_all(" ...
- 问题解决:AttributeError: ‘NoneType‘ object has no attribute ‘get‘
问题: list_links_docs = []soup = get_link_decode(URL)for link in range(10):link = soup.find("a&qu ...
- 成功解决AttributeError: 'NoneType' object has no attribute '__array_interface__'
成功解决AttributeError: 'NoneType' object has no attribute '__array_interface__' 目录 解决问题 解决思路 解决方法 原因及思路 ...
- Python之报错AttributeError:'CocaCola' object has no attribute 'local_logo'
试运行class类的代码如下,然而有报错信息:AttributeError:'CocaCola' object has no attribute 'local_logo' class CocaCola ...
- Spyder 运行出现 Reloaded modules: **AttributeError: 'NoneType' object has no attribute 'modules' 错误的解决方法
问题描述: 刚开始学习python,有很多问题不懂,网上找了很久找不到答案,就把这个问题记下来,希望可以帮助到其他初学者 使用spyder运行以下代码: 第一次运行可以正常显示结果,第二次运行时报错: ...
- rosrun rqt_graph rqt_graph报警:AttributeError: ‘ElementTree‘ object has no attribute ‘getiterator‘
一.问题描述: joes@joes:~$ rosrun rqt_graph rqt_graph Found metadata in lib /home/joes/.local/lib/python3. ...
最新文章
- 游戏平台运维自动化扩展之故障自愈
- Spring Boot下的Tomcat,你真的会用吗?
- 永远不要在代码中使用「User」这个单词
- Lesson 16.1016.1116.1216.13 卷积层的参数量计算,1x1卷积核分组卷积与深度可分离卷积全连接层 nn.Sequential全局平均池化,NiN网络复现
- python sklearn学习笔记大全(常见代码速查)
- SQL SERVER逆向工程将数据库导入PowerDesigner
- UI设计学习软件下载的好去处
- [BAT]TASKKILL 杀进程
- 查看当前Linux系统的发行版本命令详解
- 团队项目前期冲刺-7
- 基于鱼群算法的函数寻优算法
- 如何解决苹果Mac大写键CapsLock不能锁定大小写?
- 【ArcGIS自定义脚本工具】NDVI批量估算植被覆盖率
- 101113香港游有感
- 天下武功,唯快不破 - O'Reilly Velocity China 2011 PPT汇总
- 50个开机进BIOS按键查询
- fatal: unable to access ‘https://github.com/NEGVS/the-economist-ebooks.git/‘: schannel: failed to re
- java-常量和变量
- 北交 操作系统 课程笔记(一)
- 【C#基础教程】第二十章、事件
热门文章
- 更新驱动后电脑蓝屏问题解决
- 用python给MP3加封面图片,修改作者,专辑等信息
- 【项目实践】使用Vue.js和ElementUI快速实现后台管理系统的界面布局
- TLS1.2 和 TLS1.3的简要区别
- Vi编辑器完全使用手册(转)
- vue 3.0 keep-alive 失效 报错 Cannot read properties of null (reading ‘parentNode‘)
- jQuery2_事件绑定+DOM操作
- JS处理32位整型位运算
- 毕业生选题系统 c语言,[内附完整源码和文档] 毕业设计选题系统
- Golang GUI编程-图形化界面库简介