Python:如何使用“ Win32com”脚本中的事件类方法执行(Excel-button-click)
参考这篇文章 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚发现“ xlbuttonevents”是类“ buttonevents”的实例,我只需要将“ keepopen”作为类属性添加,例如:
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:
这是如何从双击按钮调用另一个函数的方法。
按钮1退出循环和脚本。
按钮2调用功能“ myFunc”:
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":