已编辑
我有一段代码可以在 wxPython WebView 中显示 html,但它只是加载 html 而没有 html 文件中的 css 和 javascript。这是我的代码。
gui.py
class MainFrame(wx.Frame):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"PlagDetect", pos = wx.DefaultPosition, size = wx.Size( 493,389 ),
self.htmlSummary = wx.html2.WebView.New(self)
page = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Summary</title>
</head>
<body>
<h1>Summary</h1>
<div id="piechart"></div>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
// Load google charts
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);
// Draw the chart and set the chart values
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 8],
['Eat', 2],
['TV', 4],
['Gym', 2],
['Sleep', 8]
]);
// Optional; add a title and set the width and height of the chart
var options = {'title':'My Average Day', 'width':550, 'height':400};
// Display the chart inside the <div> element with id="piechart"
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
</body>
</html>
"""
summary.htmlSummary.SetPage(page, "")
感谢 Saxony 的@Rolf 先生,我找到了使用 PieCtrl 而不是使用 WebView 以其他方式创建饼图的答案。答案写在下面。
最佳答案
在回答您的评论时,“是否有任何其他方法可以在 wxpython 中创建饼图”,是的,请参阅:https://wxpython.org/Phoenix/docs/html/wx.lib.agw.piectrl.PieCtrl.html
最简单的:
import wx
import wx.lib.agw.piectrl
from wx.lib.agw.piectrl import PieCtrl, PiePart
class Frame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__ (self, parent, -1, "Simple Pie Chart")
panel = wx.Panel(self, -1, size=(650,650))
# Create A Simple PieCtrl With 3 Sectors
self._pie = PieCtrl(panel, -1, wx.DefaultPosition, wx.Size(180,270))
self._pie.GetLegend().SetTransparent(True)
self._pie.GetLegend().SetHorizontalBorder(10)
self._pie.GetLegend().SetWindowStyle(wx.STATIC_BORDER)
self._pie.GetLegend().SetLabelFont(wx.Font(10, wx.FONTFAMILY_DEFAULT,
wx.FONTSTYLE_NORMAL,
wx.FONTWEIGHT_NORMAL,
False, "Courier New"))
self._pie.GetLegend().SetLabelColour(wx.Colour(0, 0, 127))
self._pie.SetHeight(10)
self._pie.SetAngle(0.35)
part = PiePart()
part.SetLabel("Label_1")
part.SetValue(300)
part.SetColour(wx.Colour(200, 50, 50))
self._pie._series.append(part)
part = PiePart()
part.SetLabel("Label 2")
part.SetValue(200)
part.SetColour(wx.Colour(50, 200, 50))
self._pie._series.append(part)
part = PiePart()
part.SetLabel("Label 3")
part.SetValue(50)
part.SetColour(wx.Colour(50, 50, 200))
self._pie._series.append(part)
self.Show()
app = wx.App()
frame = Frame(None)
app.MainLoop()
关于html - wxPython 其他创建饼图的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52024685/