使用 wxPython 控件显示 html
Display html using a wxPython control
我正在尝试在 wx python 框架的一段中显示富文本(或 html)
我试过 rtf 控件,但没有成功(参见 ). I am now trying the html route, but in the only examples I can find the html is display in a window that takes over the whole frame; for example from here
import wx
import wx.html
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(
"Here is some <b>formatted</b> <i><u>text</u></i> "
"loaded from a <font color=\"red\">string</font>.")
app = wx.PySimpleApp()
frm = MyHtmlFrame(None, "Simple HTML")
frm.Show()
app.MainLoop()
是否可以在文本框或我可以合并到我的应用程序中的其他合适的控件中显示 html?
我希望屏幕如下所示。 wx.TextCtrl 可以用 HTML window 之类的东西代替吗?
import wx
class MainFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title)
panel = MainPanel(self)
panel.txt_comments.SetValue(
"Here is some <b>formatted</b>"
"<i><u>text</u></i> "
"loaded from a "
"<font color=\"red\">string</font>.")
class MainPanel(wx.Panel):
def __init__(self, frame):
wx.Panel.__init__(self, frame)
txt_style = wx.VSCROLL|wx.HSCROLL|wx.TE_READONLY|wx.BORDER_SIMPLE
self.txt_comments = wx.TextCtrl(self, size=(300, 150), style=txt_style)
cmd_update = wx.Button(self, wx.ID_REFRESH)
main_sizer = wx.BoxSizer(wx.VERTICAL)
main_sizer.Add(self.txt_comments, flag=wx.ALL, border=10)
main_sizer.Add(cmd_update, flag=wx.ALL, border=10)
self.SetSizerAndFit(main_sizer)
app = wx.App()
frm = MainFrame(None, "Screen layout")
frm.Show()
app.MainLoop()
这一定有点接近代码的最低限度。
#!/usr/bin/env python
import wx
import wx.html as html
#----------------------------------------------------------------------
ID_New = wx.NewId()
ID_Exit = wx.NewId()
#----------------------------------------------------------------------
class MyParentFrame(wx.MDIParentFrame):
def __init__(self):
wx.MDIParentFrame.__init__(self, None, -1, "MDI Parent", size=(600,400))
self.winCount = 0
menu = wx.Menu()
menu.Append(ID_New, "&New Window")
menu.AppendSeparator()
menu.Append(ID_Exit, "E&xit")
menubar = wx.MenuBar()
menubar.Append(menu, "&File")
self.SetMenuBar(menubar)
self.CreateStatusBar()
self.Bind(wx.EVT_MENU, self.OnNewWindow, id=ID_New)
self.Bind(wx.EVT_MENU, self.OnExit, id=ID_Exit)
def OnExit(self, evt):
self.Close(True)
def OnNewWindow(self, evt):
self.winCount = self.winCount + 1
win = wx.MDIChildFrame(self, -1, "Child Window: %d" % self.winCount)
self.html = html.HtmlWindow(win, -1)
self.html.SetPage(
"Here is some <b>formatted</b> <i><u>text</u></i> "
"loaded from a <font color=\"red\">string</font>.")
#----------------------------------------------------------------------
if __name__ == '__main__':
class MyApp(wx.App):
def OnInit(self):
frame = MyParentFrame()
frame.Show(True)
self.SetTopWindow(frame)
return True
app = MyApp(False)
app.MainLoop()
我希望要注意的主要内容是:
win = wx.MDIChildFrame(self, -1, "Child Window: %d" % self.winCount)
self.html = html.HtmlWindow(win, -1)
self.html.SetPage(
"Here is some <b>formatted</b> <i><u>text</u></i> "
"loaded from a <font color=\"red\">string</font>.")
win
是您要放置 HTMLWindow 的框架。
- 注意
win
是 HTMLWindow 的第一个参数。
我几年前使用过 wxWindow,但我已经失去了大部分技能。现在我想起来了,获得优势的秘诀是从演示代码开始。这次我用了几个。
根据评论编辑:
import wx
import wx.html as html
class MainFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title)
panel = MainPanel(self)
class MainPanel(wx.Panel):
def __init__(self, frame):
wx.Panel.__init__(self, frame)
txt_style = wx.VSCROLL|wx.HSCROLL|wx.TE_READONLY|wx.BORDER_SIMPLE
self.html = html.HtmlWindow(self, -1, size=(300, 150), style=txt_style)
self.html.SetPage(
"Here is some <b>formatted</b>"
"<i><u>text</u></i> "
"loaded from a "
"<font color=\"red\">string</font>.")
app = wx.App()
frm = MainFrame(None, "Screen layout")
frm.Show()
app.MainLoop()
wx.html.HtmlWindow
或 wx.html2.WebView
很像 wxPython 中的其他子部件,因为它们需要父部件,并且需要以某种方式管理大小和位置,等等。这也意味着您可以将示例中 TextCtrl
的使用替换为这些小部件之一,并将 SetValue
替换为 SetPage
,它应该可以按照您想要的方式工作。有关详细信息,请参阅文档并尝试一下。
我正在尝试在 wx python 框架的一段中显示富文本(或 html)
我试过 rtf 控件,但没有成功(参见
import wx
import wx.html
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(
"Here is some <b>formatted</b> <i><u>text</u></i> "
"loaded from a <font color=\"red\">string</font>.")
app = wx.PySimpleApp()
frm = MyHtmlFrame(None, "Simple HTML")
frm.Show()
app.MainLoop()
是否可以在文本框或我可以合并到我的应用程序中的其他合适的控件中显示 html?
我希望屏幕如下所示。 wx.TextCtrl 可以用 HTML window 之类的东西代替吗?
import wx
class MainFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title)
panel = MainPanel(self)
panel.txt_comments.SetValue(
"Here is some <b>formatted</b>"
"<i><u>text</u></i> "
"loaded from a "
"<font color=\"red\">string</font>.")
class MainPanel(wx.Panel):
def __init__(self, frame):
wx.Panel.__init__(self, frame)
txt_style = wx.VSCROLL|wx.HSCROLL|wx.TE_READONLY|wx.BORDER_SIMPLE
self.txt_comments = wx.TextCtrl(self, size=(300, 150), style=txt_style)
cmd_update = wx.Button(self, wx.ID_REFRESH)
main_sizer = wx.BoxSizer(wx.VERTICAL)
main_sizer.Add(self.txt_comments, flag=wx.ALL, border=10)
main_sizer.Add(cmd_update, flag=wx.ALL, border=10)
self.SetSizerAndFit(main_sizer)
app = wx.App()
frm = MainFrame(None, "Screen layout")
frm.Show()
app.MainLoop()
这一定有点接近代码的最低限度。
#!/usr/bin/env python
import wx
import wx.html as html
#----------------------------------------------------------------------
ID_New = wx.NewId()
ID_Exit = wx.NewId()
#----------------------------------------------------------------------
class MyParentFrame(wx.MDIParentFrame):
def __init__(self):
wx.MDIParentFrame.__init__(self, None, -1, "MDI Parent", size=(600,400))
self.winCount = 0
menu = wx.Menu()
menu.Append(ID_New, "&New Window")
menu.AppendSeparator()
menu.Append(ID_Exit, "E&xit")
menubar = wx.MenuBar()
menubar.Append(menu, "&File")
self.SetMenuBar(menubar)
self.CreateStatusBar()
self.Bind(wx.EVT_MENU, self.OnNewWindow, id=ID_New)
self.Bind(wx.EVT_MENU, self.OnExit, id=ID_Exit)
def OnExit(self, evt):
self.Close(True)
def OnNewWindow(self, evt):
self.winCount = self.winCount + 1
win = wx.MDIChildFrame(self, -1, "Child Window: %d" % self.winCount)
self.html = html.HtmlWindow(win, -1)
self.html.SetPage(
"Here is some <b>formatted</b> <i><u>text</u></i> "
"loaded from a <font color=\"red\">string</font>.")
#----------------------------------------------------------------------
if __name__ == '__main__':
class MyApp(wx.App):
def OnInit(self):
frame = MyParentFrame()
frame.Show(True)
self.SetTopWindow(frame)
return True
app = MyApp(False)
app.MainLoop()
我希望要注意的主要内容是:
win = wx.MDIChildFrame(self, -1, "Child Window: %d" % self.winCount)
self.html = html.HtmlWindow(win, -1)
self.html.SetPage(
"Here is some <b>formatted</b> <i><u>text</u></i> "
"loaded from a <font color=\"red\">string</font>.")
win
是您要放置 HTMLWindow 的框架。- 注意
win
是 HTMLWindow 的第一个参数。
我几年前使用过 wxWindow,但我已经失去了大部分技能。现在我想起来了,获得优势的秘诀是从演示代码开始。这次我用了几个。
根据评论编辑:
import wx
import wx.html as html
class MainFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title)
panel = MainPanel(self)
class MainPanel(wx.Panel):
def __init__(self, frame):
wx.Panel.__init__(self, frame)
txt_style = wx.VSCROLL|wx.HSCROLL|wx.TE_READONLY|wx.BORDER_SIMPLE
self.html = html.HtmlWindow(self, -1, size=(300, 150), style=txt_style)
self.html.SetPage(
"Here is some <b>formatted</b>"
"<i><u>text</u></i> "
"loaded from a "
"<font color=\"red\">string</font>.")
app = wx.App()
frm = MainFrame(None, "Screen layout")
frm.Show()
app.MainLoop()
wx.html.HtmlWindow
或 wx.html2.WebView
很像 wxPython 中的其他子部件,因为它们需要父部件,并且需要以某种方式管理大小和位置,等等。这也意味着您可以将示例中 TextCtrl
的使用替换为这些小部件之一,并将 SetValue
替换为 SetPage
,它应该可以按照您想要的方式工作。有关详细信息,请参阅文档并尝试一下。