循环遍历每个组合框(我有四个)并抓取它的文本的最佳解决方案是什么?
一些文档(通过c#)说要创建一个组合框列表,但我不确定如何通过.NET框架将其移植到Pyhton。我所做的一切似乎都会产生一些错误。
我可以遍历所有的控件,但研究表明这是不必要的,并且会消耗CPU时间。
说了这么多,下面是我到目前为止没有成功的事情。我去掉了代码的其他部分来关注这一点:
import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
clr.AddReference("System.Data")
clr.AddReference("System.Globalization")
clr.AddReference("System.Collections")
from System.Windows.Forms import *
from System.Drawing import *
from System.Data import *
from System.Data.SqlClient import *
from System.Globalization import *
from System.Collection.Generics import *
def FindPopulatedDropDowns():
# https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-5.0
Items = List[string]()
comboBoxes = OfType[ComboBox]().Where(x in x.Name.StartsWith('comboBox'))
for cmb in comboBoxes:
for cmbitem in cmb.Items:
print(cmbitem.ToString())有没有人有办法做到这一点?
发布于 2021-03-31 05:23:33
这是我的解决方案。我相信还有更优雅的方式,但这可以满足我的需要。
def FindPopulatedDropDowns(self):
# https://docs.microsoft.com/en-us/dotnet/api/system.object.gettype?view=net-5.0#System_Object_GetType
for c in self.Controls:
if c.GetType() == ComboBox:
print(c.Text)https://stackoverflow.com/questions/66871110
复制相似问题