Python:如何使用“ Win32com”脚本中的事件类方法执行(Excel-button-click)

发布于 2025-01-26 14:07:08 字数 1578 浏览 2 评论 0原文

参考这篇文章 excel按钮单击python 将消息打印到控制台,而是调用函数或更改变量值(请参阅下面的测试标记)。

由于“ win32.withevents”需要一个“类”,而不是其参数中的“类实例”,因此我想知道如何访问此按钮使用的“ buttonevent”的此类实例以获取返回值或更改变量?

在下面的示例中,我使用“全局keepopen”退出时环。但是使用全球不是一个好风格。如果没有“全局”,我如何才能更改“ keepopen”变量?

我基本上想在单击按钮时更改变量或调用其他功能/类方法。

我该如何实现?

import win32com.client as win32
import pythoncom
import sys

# use absolute pathes
WORKBOOK = "d:/LabTestPy/labPyCommon/test/test_excel_RegDict.xlsm"
WORKSHEET = "Sheet"

# the button event will open this class
class ButtonEvents:
    # method executed on doubleclick to close while loop
    def OnDblClick(self, *args):
        print("button double clicked")
        global keepOpen
        keepOpen = False

#config win32com excel connection
xlApp = win32.gencache.EnsureDispatch('Excel.Application')
xlApp.Visible = True
xlWb = xlApp.Workbooks.Open(WORKBOOK)
xlWs = xlWb.Sheets(WORKSHEET)
# define button event callback class
xlButtonEvents=win32.WithEvents(xlWs.OLEObjects("CommandButton1").Object,ButtonEvents)

# a global variable to exit the while-loop
global keepOpen
keepOpen = True

# a while loop to wait until the button in excel is double-clicked
while keepOpen:  #How to avoid a global variable here ?
    pythoncom.PumpWaitingMessages()

    # How to execute a function here, when another button is pushed ?


print("Script finished - closing Excel")

xlWb.Close(False) # False: do not save on close, True: save on close
xlApp.Quit()

In reference to this post Excel button click event in Python I want to not only print out a message to the console, but instead call a function or change a variable value (see below my test-script).

Since the "win32.WithEvents" requires a "class" instead of a "class instance" in its arguments, I am wondering how to access this class instance of the "ButtonEvent" used by this button to get a return value or change a variable ?

In the below example, I am using the "global keepOpen" to exit the while-loop. but using globals is not a good style. How can I change the "keepOpen" variable without "global" ?

I basically would like to change variables or call other functions/class-methods when the button is clicked.

How can I achieve this ?

import win32com.client as win32
import pythoncom
import sys

# use absolute pathes
WORKBOOK = "d:/LabTestPy/labPyCommon/test/test_excel_RegDict.xlsm"
WORKSHEET = "Sheet"

# the button event will open this class
class ButtonEvents:
    # method executed on doubleclick to close while loop
    def OnDblClick(self, *args):
        print("button double clicked")
        global keepOpen
        keepOpen = False

#config win32com excel connection
xlApp = win32.gencache.EnsureDispatch('Excel.Application')
xlApp.Visible = True
xlWb = xlApp.Workbooks.Open(WORKBOOK)
xlWs = xlWb.Sheets(WORKSHEET)
# define button event callback class
xlButtonEvents=win32.WithEvents(xlWs.OLEObjects("CommandButton1").Object,ButtonEvents)

# a global variable to exit the while-loop
global keepOpen
keepOpen = True

# a while loop to wait until the button in excel is double-clicked
while keepOpen:  #How to avoid a global variable here ?
    pythoncom.PumpWaitingMessages()

    # How to execute a function here, when another button is pushed ?


print("Script finished - closing Excel")

xlWb.Close(False) # False: do not save on close, True: save on close
xlApp.Quit()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

反差帅 2025-02-02 14:07:08

我刚刚发现“ xlbuttonevents”是类“ buttonevents”的实例,我只需要将“ keepopen”作为类属性添加,例如:

import win32com.client as win32
import pythoncom
import sys

# use absolute pathes
WORKBOOK = "d:/LabTestPy/labPyCommon/test/test_excel_RegDict.xlsm"
WORKSHEET = "Sheet"

# the button event will open this class
class ButtonEvents:
    
    def __init__(self):
        self.keepOpen = True        
        self.myvar = 1
        
    # method executed on doubleclick to close while loop
    def OnDblClick(self, *args):
        print("button double clicked")
        self.keepOpen = False
        self.myvar = 3
        

#config win32com excel connection
xlApp = win32.gencache.EnsureDispatch('Excel.Application')
xlApp.Visible = True
xlWb = xlApp.Workbooks.Open(WORKBOOK)
xlWs = xlWb.Sheets(WORKSHEET)
# define button event callback class
xlButtonEvents=win32.WithEvents(xlWs.OLEObjects("CommandButton1").Object,ButtonEvents)
    
# a while loop to wait until the button in excel is double-clicked
while xlButtonEvents.keepOpen:
    pythoncom.PumpWaitingMessages()

    print(xlButtonEvents.myvar)

print("Script finished - closing Excel")

xlWb.Close(False) # False: do not save on close, True: save on close
xlApp.Quit()

I just found out that "xlButtonEvents" is the instance of the class "ButtonEvents", and I simply needed to add "keepOpen" as a class attribute, like this:

import win32com.client as win32
import pythoncom
import sys

# use absolute pathes
WORKBOOK = "d:/LabTestPy/labPyCommon/test/test_excel_RegDict.xlsm"
WORKSHEET = "Sheet"

# the button event will open this class
class ButtonEvents:
    
    def __init__(self):
        self.keepOpen = True        
        self.myvar = 1
        
    # method executed on doubleclick to close while loop
    def OnDblClick(self, *args):
        print("button double clicked")
        self.keepOpen = False
        self.myvar = 3
        

#config win32com excel connection
xlApp = win32.gencache.EnsureDispatch('Excel.Application')
xlApp.Visible = True
xlWb = xlApp.Workbooks.Open(WORKBOOK)
xlWs = xlWb.Sheets(WORKSHEET)
# define button event callback class
xlButtonEvents=win32.WithEvents(xlWs.OLEObjects("CommandButton1").Object,ButtonEvents)
    
# a while loop to wait until the button in excel is double-clicked
while xlButtonEvents.keepOpen:
    pythoncom.PumpWaitingMessages()

    print(xlButtonEvents.myvar)

print("Script finished - closing Excel")

xlWb.Close(False) # False: do not save on close, True: save on close
xlApp.Quit()
水水月牙 2025-02-02 14:07:08

这是如何从双击按钮调用另一个函数的方法。
按钮1退出循环和脚本。
按钮2调用功能“ myFunc”:

import win32com.client as win32
import pythoncom

# use absolute pathes
WORKBOOK = "d:/LabTestPy/labPyCommon/test/test_excel_RegDict.xlsm"
WORKSHEET = "Sheet"

def myfunc(myarg):
    print("myfunc was executed " + myarg)


# the button 1 will exit the loop
class Button1Events:    
    def __init__(self):
        self.keepOpen = True        
    # method executed on doubleclick to close while loop
    def OnDblClick(self, *args):
        print("button1 double clicked")
        self.keepOpen = False

# button 2 will call a function
class Button2Events:    
    def OnDblClick(self, *args):
        print("button2 double clicked")
        myfunc("from Button 2")

#config win32com excel connection
xlApp = win32.gencache.EnsureDispatch('Excel.Application')
xlApp.Visible = True
xlWb = xlApp.Workbooks.Open(WORKBOOK)
xlWs = xlWb.Sheets(WORKSHEET)
# define button event callback class
xlButton1Events=win32.WithEvents(xlWs.OLEObjects("CommandButton1").Object,Button1Events)
xlButton2Events=win32.WithEvents(xlWs.OLEObjects("CommandButton2").Object,Button2Events)

# a while loop to wait until the button in excel is double-clicked
while xlButton1Events.keepOpen:
    pythoncom.PumpWaitingMessages()


print("Script finished - closing Excel")

xlWb.Close(False) # False: do not save on close, True: save on close
xlApp.Quit()

Here is how to call another function from a double clicked button.
Button 1 exits the loop and script.
Button 2 calls the function "myfunc":

import win32com.client as win32
import pythoncom

# use absolute pathes
WORKBOOK = "d:/LabTestPy/labPyCommon/test/test_excel_RegDict.xlsm"
WORKSHEET = "Sheet"

def myfunc(myarg):
    print("myfunc was executed " + myarg)


# the button 1 will exit the loop
class Button1Events:    
    def __init__(self):
        self.keepOpen = True        
    # method executed on doubleclick to close while loop
    def OnDblClick(self, *args):
        print("button1 double clicked")
        self.keepOpen = False

# button 2 will call a function
class Button2Events:    
    def OnDblClick(self, *args):
        print("button2 double clicked")
        myfunc("from Button 2")

#config win32com excel connection
xlApp = win32.gencache.EnsureDispatch('Excel.Application')
xlApp.Visible = True
xlWb = xlApp.Workbooks.Open(WORKBOOK)
xlWs = xlWb.Sheets(WORKSHEET)
# define button event callback class
xlButton1Events=win32.WithEvents(xlWs.OLEObjects("CommandButton1").Object,Button1Events)
xlButton2Events=win32.WithEvents(xlWs.OLEObjects("CommandButton2").Object,Button2Events)

# a while loop to wait until the button in excel is double-clicked
while xlButton1Events.keepOpen:
    pythoncom.PumpWaitingMessages()


print("Script finished - closing Excel")

xlWb.Close(False) # False: do not save on close, True: save on close
xlApp.Quit()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文