使用wx.html2时如何应用标签条件?

时间:2017-08-28 08:31:56

标签: python-3.x wxpython wx.html2

  1. 我将条件表达式放在{2},{3}中,并尝试在Ture的情况下为绿色,在False的情况下为红色。是否可以使用SetForegroundColour来表示颜色?如果你运行它,它将是None。你还需要另一种方法吗?

  2. 我还尝试background-image: url ('1.png');指定背景,它是白色背景。在body标签内使用<img scr = 1.png />时,会出现x显示图像错误。

  3. - - 编码:utf-8 - -

    导入wx

    导入wx.html2

    导入sqlite3

    class AppFrame(wx.Frame):    
        def __init__(self, parent):    
            super(AppFrame, self).__init__(parent)    
            self.html_view = wx.html2.WebView.New(self)    
            sizer = wx.BoxSizer(wx.VERTICAL)    
            sizer.Add(self.html_view, 1, wx.EXPAND)    
            self.SetSizer(sizer)    
    
    if __name__ == '__main__':    
        i = 'test1'    
        j = 'test2'    
        k = ('True', 'False')    
        l = ('True(color Green)', 'False(color Red)', 'How??')    
    
        for loop1 in k: pass    
        if loop1 == 'True': a = print(k[0])    
        else: a = print(k[1])    
    
        app = wx.App()    
        frame = AppFrame(None)    
        frame.html_view.SetPage("""    
    <!doctype html>    
    <html lang="ko">    
      <head>    
        <meta charset="utf-8">    
        <title>HTML TEST</title>    
        <style>    
          table{{    
            width: 100%;    
            border-collapse: collapse    
          }}    
          table, th, td{{    
            border: 2px solid #bcbcbc;    
            text-align: center;    
          }}    
          body{{    
            background-image:url('1.png');    
            background-position: conter conter;    
            background-color:gold;    
          }}    
        </style>    
      </head>    
      <body>    
        <table>    
          <caption><h1>Hello world!<h1></caption>    
          <thead>    
            <tr>          
              <th>{0}</th>    
              <th>{1}</th>    
              <th>Ipsum</th>    
              <th>Ipsum</th>    
              <th>Ipsum</th>    
              <th>Ipsum</th>      
            </tr>    
          </thead>    
          <tbody>    
            <tr>          
              <td>{2}</td>    
              <td>{3}</td>    
              <td>Dolor</td>    
              <td>Dolor</td>    
              <td>Dolor</td>    
              <td>Dolor</td>      
            </tr>    
          </tbody>    
          <tfoot>    
            <tr>    
              <td colspan="6">Table Foot</td>    
            </tr>    
          </tfoot>      
        </table>    
        <script>    
            if({0} == 'True'){{    
                ???    
            }}      
            else{{    
                ???    
            }}      
        </script>    
      </body>    
    </html>    
    """.format(i, j, a, l), "")    
        frame.Show(True)    
        app.MainLoop()
    

    enter image description here

1 个答案:

答案 0 :(得分:1)

要理解你想要什么有点困难,但这里是使用.format的HTML元素的条件背景更改的最简单示例。建议修改Python 3的示例.CSS片段background-color可用于支持它的任何元素,例如<th></th>元素。如果要更改文本颜色,请使用color。同样可以应用于HTML代码的<style>部分。

import wx
import wx.html2
class AppFrame(wx.Frame):
    def __init__(self, parent):
        super(AppFrame, self).__init__(parent)
        self.html_view = wx.html2.WebView.New(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.html_view, 1, wx.EXPAND)
        self.SetSizer(sizer)

if __name__ == '__main__':

    Test = True

    if Test == True:
        color = 'Green'
    else:
        color = 'Red'

    app = wx.App()
    frame = AppFrame(None)
    frame.html_view.SetPage("""
            <!doctype html>
            <html lang="ko">
              <head>
                <meta charset="utf-8">
                <title>HTML TEST</title>
                <style>
                  body{{
                    background-position: conter conter;
                    background-color:gold;
                  }}
                </style>
              </head>
              <body>
              <p>Normal text</p>
              </br>
              <p style="background-color:{0}">Conditionally changed background for Test={1}</p>
              </br>
              </body>
            </html>""".format(color, Test), "")
    frame.Show(True)
    app.MainLoop()

Conditionally changed background Test=True