2017-09-18 77 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執行新的數據值。