gpt4 book ai didi

html - 使用 wxPython 控件显示 html

转载 作者:行者123 更新时间:2023-12-04 03:03:17 25 4
gpt4 key购买 nike

我正在尝试在 wx python 框架的一段中显示富文本(或 html)

我试过 rtf 控件,但没有成功(参见 here)。我现在正在尝试 html 路由,但在唯一的示例中,我可以找到 html 显示在一个占据整个框架的窗口中;例如来自 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?

我希望屏幕看起来像下面这样。可以用 HTML 窗口或其他东西替换 wx.TextCtrl 吗?

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()

关于html - 使用 wxPython 控件显示 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46999890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com