【问题标题】:How to use a custom font in pywebview如何在 pywebview 中使用自定义字体
【发布时间】:2020-12-24 18:25:43
【问题描述】:

我想在pywebview 应用程序中使用自定义字体,但添加 CSS 后,仅使用备用字体。字体文件位于/css/fonts 目录中,但相对路径或绝对路径都不起作用。

我的 CSS:

@font-face {
  font-family: 'Retro Gaming';
  src: url('fonts/retro_gaming.ttf') format('truetype'),
    url('fonts/retro_gaming-webfont.woff2') format('woff2'),
    url('fonts/retro_gaming-webfont.woff') format('woff');
  font-weight: normal;
  font-style: normal;
}

body {
  font-family: 'Courier New', Courier, monospace;
}

.menu {
  font-family: 'Retro Gaming', Courier, monospace;
}

我的 HTML:

<html>

<body>
  <p class="menu">This font should be Retro Gaming</p>
  <p>This font should be Courier New</p>
</body>

</html>

我在启动应用程序时看到的消息是:

127.0.0.1 - - [24/Dec/2020 18:13:11] "GET / HTTP/1.1" 200 200
127.0.0.1 - - [24/Dec/2020 18:13:11] "GET /fonts/retro_gaming.ttf HTTP/1.1" 404 42
127.0.0.1 - - [24/Dec/2020 18:13:11] "GET /fonts/retro_gaming-webfont.woff2 HTTP/1.1" 404 52
127.0.0.1 - - [24/Dec/2020 18:13:11] "GET /fonts/retro_gaming-webfont.woff HTTP/1.1" 404 51

编辑: 带有字体文件路径的图像:

【问题讨论】:

    标签: html css fonts pywebview


    【解决方案1】:

    如果您的 html 代码在另一个文件中并且 css 也像这样 ../fonts/retro_gaming.ttf 在我确实喜欢您的路径时它对我有用,请尝试在您的位置文件之前添加 ../

    【讨论】:

    • 相同的 404 结果。
    • 能否给个字体文件的图片和路径
    • 当然,我用图片更新了我的问题。
    【解决方案2】:

    好的,我想通了。通过将字体文件放在 html 所在的同一目录中(在我的情况下为assets),我可以正常工作。您甚至可以为其中的字体创建子目录。

    编辑:另一种选择是避免将样式加载到 python 代码中的窗口并直接从 HTML 中执行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-23
      • 2017-01-01
      • 2011-08-04
      • 2011-08-02
      • 2013-12-14
      • 2012-06-19
      • 2014-01-23
      相关资源
      最近更新 更多