使用 wxPython 在 Python 中的文本上的鼠标事件 [英] Mouse events on text in Python using wxPython

查看:35
本文介绍了使用 wxPython 在 Python 中的文本上的鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的:

我创建了一个窗口,上面显示了文本,作为用户,我点击了文本,示例:显示的文本是.

I create a window and there is text that is displayed on it, as a user I click on the text, example: the displayed text is.

'Hello World, I am a Python program.'

所以如果用户点击单词,我希望它生成一个事件,它会进入一个函数,我想在函数中做一些事情(比如改变那个词的颜色,所以我还需要跟踪我点击了哪个单词)

So if the user clicks words, I want it to generate an event and it would go into a function and I want to do something in the function (like changing the color of that word, so I also need to track which word I clicked)

我不太确定该怎么做,我可以将每个单词都变成一个按钮,但这会很丑.

I am not so sure how to do that, I could potentially make each word a button but that would be ugly.

推荐答案

import wx
def SomeListener(evt):
    print "Got Event:",evt
    print "My XY:",evt.GetX(),evt.GetY()
    #youll have to figure out which word you clicked using x,y (note x,y relative to static text field)
a= wx.App(redirect=False)
f = wx.Frame(None,-1)
p = wx.Panel(f,-1)
t = wx.StaticText(p,-1,"Some Text")
t.Bind(wx.EVT_LEFT_DOWN,SomeListener)
f.Show()
a.MainLoop()

或使用 htmlwin ......但它强调了所有的话......我无法弄清楚如何不这样做

or using htmlwin ... but it underlines all the words... I wasnt able to figure out how to not do that

import wx

import wx.html
def OnClickWord(e):
    print "You Clicked:",e.GetLinkInfo().GetHref()
    return
class MyHtmlFrame(wx.Frame):

    def __init__(self, parent, title):

        wx.Frame.__init__(self, parent, -1, title)

        html = wx.html.HtmlWindow(self)

        #if "gtk2" in wx.PlatformInfo:

        html.SetStandardFonts()

        html.SetPage(
        "<style>a {text-decoration: none;color: #000; }</style>" #sorry no css support :/
        "<a href=\"word1\">Word1</a> <a href=\"word2\">word 2</a> <a href=\"wizard of oz\">wizard of oz</a>.")

app = wx.PySimpleApp()

frm = MyHtmlFrame(None, "Simple HTML")
frm.Bind(wx.html.EVT_HTML_LINK_CLICKED,OnClickWord)
frm.Show()

app.MainLoop()

这篇关于使用 wxPython 在 Python 中的文本上的鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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