2011-10-03 66 views
1

爲簡潔起見:我試圖用wxPython實現this,但我努力將這些代碼放入基於wxPython的腳本中。wxPython和windows 7任務欄

我的簡單PyQt測試代碼工作正常。那就是:

from PyQt4 import QtGui 
from threading import Thread 
import time 
import sys 
import comtypes.client as cc 
import comtypes.gen.TaskbarLib as tbl 

TBPF_NOPROGRESS = 0 
TBPF_INDETERMINATE = 0x1 
TBPF_NORMAL = 0x2 
TBPF_ERROR = 0x4 
TBPF_PAUSED = 0x8 

cc.GetModule("taskbar.tlb") 
taskbar = cc.CreateObject("{56FDF344-FD6D-11d0-958A-006097C9A090}", interface=tbl.ITaskbarList3) 

class MainWindow(QtGui.QMainWindow): 
    def __init__(self, parent=None): 
     QtGui.QMainWindow.__init__(self, parent) 
     self.setWindowTitle("Test") 

     self.progress_bar = QtGui.QProgressBar(self) 
     self.setCentralWidget(self.progress_bar) 
     self.progress_bar.setRange(0, 100) 

     self.progress = 0 

     self.show() 

     thread = Thread(target=self.counter) 
     thread.setDaemon(True) 
     thread.start() 

    def counter(self): 
     while True: 
      self.progress += 1 
      if self.progress > 100: 
       self.progress = 0 

      time.sleep(.2) 

      self.progress_bar.setValue(self.progress) 

      taskbar.HrInit() 
      hWnd = self.winId() 
      taskbar.SetProgressState(hWnd, TBPF_ERROR)   
      taskbar.SetProgressValue(hWnd, self.progress, 100) 

app = QtGui.QApplication(sys.argv) 
ui = MainWindow() 
sys.exit(app.exec_()) 

但是,當我嘗試執行wxPython的對應,如預期任務欄無法正常工作。這裏的wxPython的代碼:

import wx 
import time 
import comtypes.client as cc 
import comtypes.gen.TaskbarLib as tbl 
from threading import Thread 

TBPF_NOPROGRESS = 0 
TBPF_INDETERMINATE = 0x1 
TBPF_NORMAL = 0x2 
TBPF_ERROR = 0x4 
TBPF_PAUSED = 0x8 

cc.GetModule("taskbar.tlb") 
taskbar = cc.CreateObject("{56FDF344-FD6D-11d0-958A-006097C9A090}", interface=tbl.ITaskbarList3) 

class MainWindow(wx.Frame): 
    def __init__(self, parent, ID, title): 
     wx.Frame.__init__(self, parent, ID, title) 

     self.panel = wx.Panel(self) 
     self.gauge = wx.Gauge(self.panel) 
     self.gauge.SetValue(0) 

     self.progress = 0 

     self.Show() 

     thread = Thread(target=self.counter) 
     thread.setDaemon(True) 
     thread.start() 

    def counter(self): 
     while True: 
      self.progress += 1 
      if self.progress > 100: 
       self.progress = 0 

      time.sleep(.2) 

      self.gauge.SetValue(self.progress) 

      taskbar.HrInit() 
      hWnd = self.GetHandle() 

      taskbar.SetProgressState(hWnd, TBPF_ERROR) 
      taskbar.SetProgressValue(hWnd, self.progress, 100) 

app = wx.PySimpleApp() 
frame = MainWindow(None, wx.ID_ANY, "Test") 
app.SetTopWindow(frame) 
app.MainLoop() 

特別是我認爲這個問題是由於wxWindow的窗口句柄(HWND)方法,從它的Qt相當於不同,前者返回一個整數,後者是「sip.voidptr目的」。

問題是我已經用wxPython編寫了整個代碼(1200+行),因此我不能重新編寫它來使用Qt(而不是談論不同的許可證)。

您對此有何看法?我應該放棄嗎?
感謝很多提前:)

編輯

感謝羅伯特·奧康納,現在它的工作原理。但是,我仍然無法得到爲什麼GetHandle返回一個整數,而winId返回一個對象。在.idl文件中,參數hwnd在所有函數定義中聲明爲long。也許這也是一個簡單的問題;)任何想法?

+0

PySide和PyQT有相同的許可證... – JBernardo

+0

謝謝,我會在將來考慮它 –

回答

1

在下面一行:

hWnd = self.panel.GetId() 

你想用GetHandle(),而不是GetId()

編輯:這是最初發表的評論,但我認爲這將是更適合我重新發布作爲答案。

關於編輯你的問題:如果現在的工作我想有不是一個問題了;)好了,認真雖然..

INTS和多頭都統一在Python,如果我猜comtypes可能會在後臺做一些強制。我不知道在處理一般的comtypes時是否有必要擔心這種細節,但在這種情況下似乎並不重要。

現在我沒有使用PyQT的經驗,但是在Python中,您可以在諸如__int____long__之類的對象上定義特殊方法來模擬Ints和Long。如果我不得不猜測,你在PyQT中獲得的對象定義了其中一種方法。

+0

對不起,我在談論GetHandle,但忘記編輯代碼後,我做了各種測試:)事實上是現在它的工作原理...我已經做了很多與我的代碼混淆,我的錯。我編輯問題。謝謝! –

+0

我確實想指出,我很感謝你問這個問題。我有幾個想要使用任務欄功能的原因,現在我不必去計算_how_。現在我必須再添加一個理由到我的「需要IDL編譯器的原因列表」列表中。 –

+0

好吧,很高興我能幫忙,再次感謝你!我絕對沒有想到__int__方法。我檢出(「dir-ed」對象實例)並發現它:'int(hWnd)'返回一個合理的值。這一切似乎都更清晰了:) –