2017-09-18 76 views
0
# -*- coding: utf-8 -*- 
import wx.html2 
import sqlite3 

class MainFream(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, None, style=wx.DEFAULT_FRAME_STYLE) 
     self.htmlweb = wx.html2.WebView.New(self, size=(0, 0), 
              backend=wx.html2.WebViewBackendIE 
              #backend=wx.html2.WebViewBackendWebKit 
     ) 

     self.conn = sqlite3.connect("main.db") 
     self.cursor = self.conn.cursor() 

     self.timer = wx.Timer(self) 
     self.Bind(wx.EVT_TIMER, self.AutoRefresh, self.timer) 
     self.timer.Start(10000) 

     self.HtmlCode() 

    def AutoRefresh(self, event): 
     self.HtmlCode() 

    def DataBase(self): 
     self.color = ('#00ff00', 'red') 
     self.selectcolor = [] 

     self.cursor.execute('select *from clinic1') 
     for self.data1 in self.cursor.fetchall(): pass 
     for n in range(4, 7): 
      if self.data1[n] == 'True': self.selectcolor.append(self.color[0]) 
      else: self.selectcolor.append(self.color[1]) 

     self.cursor.execute('select *from clinic2') 
     for self.data2 in self.cursor.fetchall(): pass 
     for n in range(4, 7): 
      if self.data2[n] == 'True': self.selectcolor.append(self.color[0]) 
      else: self.selectcolor.append(self.color[1]) 

     self.cursor.execute('select *from clinic3') 
     for self.data3 in self.cursor.fetchall(): pass 

    def HtmlCode(self): 
     self.DataBase() 
     self.HTML_CODE = """ 
     <!DOCTYPE HTML> 
     <html lang="ko"> 
      <head> 
      <meta charset='utf-8'> 
      <title>HTML TEST</title> 
      <style type='text/css'> 
       html{{ 
       background-image:url(D:/python3/PycharmProjects/untitled/3.png); 
       }} 
       table{{ 
       width:100%; 
       }} 
       table, caption, th, td{{  
       border:2px solid lightgray;  
       border-collapse:collapse; 
       height:0px; 
       color:white;   
       text-align:center;  
       font-size:155%;  
       font-style:normal; 
       font-weight:bold; 
       font-family:Malgun Gothic;      
       }} 
       caption{{ 
       background-color:white; color:#0d0d0d; font-size:250%; font-weight:bold; 
       }} 
       th{{ 
       background-color:white; color:#0d0d0d; font-size:145%; font-weight:bold; 
       }}  
       caption, th{{ 
       background-image:url(D:/python3/PycharmProjects/untitled/bg5.png); 
       }}  
      </style> 
      </head> 
      <body scroll='no'> 
      <table> 
       <caption>T E S T Main</caption> 
       <thead> 
       <tr bgcolor='ffffff'>  
        <th>T E S T1</th> 
        <th>T E S T2</th> 
        <th>T E S T3</th> 
        <th>T E S T4</th> 
        <th>T E S T5</th> 
        <th>T E S T6</th> 
       </tr> 
       </thead> 
       <tbody> 
       <tr style='background: url(D:/python3/PycharmProjects/untitled/bg1.png)'> 
        <td>{0}</td> 
        <td>{1}</td> 
        <td>{2}</td>  
        <td><p style='color:{3}'>{4}</p></td> 
        <td><p style='color:{5}'>{6}</p></td> 
        <td><p style='color:{7}'>{8}</p></td>  
       </tr> 
       <tr style='background: url(D:/python3/PycharmProjects/untitled/bg1.png)'>  
        <td>{9}</td> 
        <td>{10}</td> 
        <td>{11}</td>  
        <td><p style='color:{12}'>{13}</p></td> 
        <td><p style='color:{14}'>{15}</p></td> 
        <td><p style='color:{16}'>{17}</p></td>  
       </tr>   
       </tbody> 
       <tfoot> 
       <tr> 
        <td colspan="6"><marquee><font color='cyan'>{18}</font></marquee></td> 
       </tr> 
       </tfoot> 
      </table> 
      </body> 
     </html> 
     """ 
     self.DataIn() 

    def DataIn(self): 
     self.htmlweb.SetPage(self.HTML_CODE.format(
      self.data1[1], self.data1[2], self.data1[3], self.selectcolor[0], self.data1[4], self.selectcolor[1], self.data1[5], self.selectcolor[2], self.data1[6], 
      self.data2[1], self.data2[2], self.data2[3], self.selectcolor[3], self.data2[4], self.selectcolor[4], self.data2[5], self.selectcolor[5], self.data2[6], 
      self.data3[1]), "") 

if __name__ == '__main__': 
    app = wx.App() 
    fream = MainFream() 
    fream.Show(True) 
    app.MainLoop() 

我们使用Windows来测试屏幕装载数据库使用wx.html2wx.timer每小时加载它。我想避免闪烁时,我wx.timer

每次我使用wx.timer加载数据时,背景图像在background-image: url (D: /python3/PycharmProjects/untitled/3.png);上闪烁白色。

难道你不能解决闪烁的问题吗?

此外,我不想使用wx.timer,但我只想在仅更改数据库值时实时反映更改的内容。在这种情况下是否有一个模块?

回答

1

闪烁发生是因为您每次都重新加载整个页面,所以窗口小部件正在转储当前页面,清除窗口并重新绘制新文档。如果你想不经过所有的更新,你需要做一些类似的事情来解决实际网站上的问题,例如使用Javascript从服务器获取新值,然后更新现有的文档对象并显示新的文本。

由于您使用的是WebView,因此有一些选项可以实现这一点,它比需要在某处运行真正的Web服务器稍微简单一些。一个选择是在你的应用程序中只有另一个线程正在监听http连接,并让它返回一些带有新值的json以响应JavaScript代码请求。或者,由于WebView具有方便的RunScript,您可以通过一些JavaScript代码为您的应用程序代码中的WebView执行新的数据值。