前几天,我惊讶地发现,wxPython面板上的静态文本无法选择/突出显示。我需要这样,以便用户可以复制内容。
我没有找到标志或类似内容来使StaticText
组件可供选择。
import wx
app = wx.App(False)
frame = wx.Frame(None, wx.ID_ANY, "Hello, World!")
panel = wx.Panel(frame)
panel.SetBackgroundColour("#FFB6C1")
st = wx.StaticText(panel, wx.ID_ANY, "Hello, World!")
frame.Show(True)
app.MainLoop()
答案 0 :(得分:1)
我找不到让StaticText
可选择/可复制的方法,但这里有2个黑客可以解决问题。
1.单击鼠标右键复制到剪贴板
2. TextCtrl
伪装成StaticText
import wx
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1)
self.panel = wx.Panel(self)
self.st = wx.StaticText(self.panel, wx.ID_ANY, "Hello, World! Copy me with right click",pos=(5,5))
self.st.Bind(wx.wx.EVT_RIGHT_DOWN,self.ShowPopup)
self.tc = wx.TextCtrl(self.panel, wx.ID_ANY, "Paste here", pos=(5,30),size=(300,20))
self.tc2 = wx.TextCtrl(self.panel, wx.ID_ANY,"TextCtrl masquerading as StaticText - Select me",style=wx.TE_READONLY|wx.NO_BORDER,pos=(5,60),size=(300,20))
self.tc2.SetBackgroundColour(self.panel.GetBackgroundColour())
self.Show()
def ShowPopup(self,event):
popmenu = wx.Menu()
popmenu.Append(1, "Copy this text to clipboard")
popmenu.Bind(wx.EVT_MENU, self.Copy)
self.panel.PopupMenu(popmenu)
popmenu.Destroy()
def Copy(self,event):
clipdata = wx.TextDataObject()
text = self.st.GetLabelText()
clipdata.SetText(text)
wx.TheClipboard.Open()
wx.TheClipboard.SetData(clipdata)
wx.TheClipboard.Close()
app = wx.App()
frame = MyFrame()
app.MainLoop()
答案 1 :(得分:0)
经过几次实验后,我决定创建自定义的支持HTML的工具提示窗口。这是gist的当前版本(希望允许嵌入而不是复制此内容):
import wx
import wx.html
import webbrowser
class HtmlPopupTransientWindow(wx.PopupTransientWindow):
def __init__(self, parent, style, html_body_content, bgcolor, size):
wx.PopupTransientWindow.__init__(self, parent, style)
panel = wx.Panel(self)
panel.SetBackgroundColour(bgcolor)
html_window = self.HtmlWindow(panel, wx.ID_ANY, size=size)
html_window.SetPage('<body bgcolor="' + bgcolor + '">' + html_body_content + '</body>')
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(html_window, 0, wx.ALL, 5)
panel.SetSizer(sizer)
sizer.Fit(panel)
sizer.Fit(self)
self.Layout()
class HtmlWindow(wx.html.HtmlWindow):
def OnLinkClicked(self, link):
# get a hold of the PopupTransientWindow to close it
self.GetParent().GetParent().Dismiss()
webbrowser.open(link.GetHref())
有关详细说明,请参阅https://frightanic.com/software-development/create-an-info-icon-with-wxpython/。