爲簡潔起見:我試圖用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
。也許這也是一個簡單的問題;)任何想法?
PySide和PyQT有相同的許可證... – JBernardo
謝謝,我會在將來考慮它 –