Python - “'function' object is not iterable” in GUI Combobox with multiple functions case

Python - "'function' object is not iterable " in GUI Combobox with multiple functions case

代码的用途是

  1. 对于三个问题中的每一个(组合框样式的 GUI 界面),我必须选择一个项目。
  2. 对应问题的每一项代表一组数字。
  3. 根据我选择的三个问题,代码将计算集合的交集并在屏幕上显示答案。

Vall、Vcs、V360...是整数向量。假设他们是给定的。例如:Vall = [6,8,13,17,23,38]... 当然,所有的向量都是相同的维度。

代码:

import openpyxl
import numpy as np
from tkinter import *
import tkinter.ttk as ttk
from openpyxl import Workbook  # or "from openpyxl import Workbook": create workbook
from openpyxl import load_workbook # open/excess excel file or spreadsheet existed
from IPython.display import clear_output

# The following defines the function for each question shown on screen. 

def selected1(event):   # Type 
    if mycombobox.get() == 'Type':
        return Vall
    if mycombobox.get() == 'clam shell':
        return Vcs
    if mycombobox.get() == '360\u00b0':
        return V360

def selected2(event):   # WLAN1
    if mycombobox.get() == 'WLAN1 ant. position':
        return Vall
    if mycombobox1.get() == 'hinge':
        return Vhinge1
    if mycombobox1.get() == 'top':
        return Vtop1

def selected3(event):    # WLAN2
    if mycombobox.get() == 'WLAN2 ant. position':
        return Vall
    if mycombobox2.get() == 'hinge':
        return Vhinge2
    if mycombobox2.get() == 'top':
        return Vtop2

# The following function does the intersection of three sets obtained above. 

def set_intersection(xx1,xx2,xx3):
    collect_all = [xx1,xx2,xx3]
    recc = set.intersection(*map(set,collect_all))   # <----------------------
    my_list = list(recc)
    my_list.sort()
    return my_list

# ================== The main part: GUI. ====================== 

root = Tk()
root.title('Industry Solution')
root.geometry("500x800")

#  === Type (first question: type of computer), three choices: Type, clam shell... ===

label_a = Label(root, text="1. type of computer")
label_a.pack(side = TOP, pady=1) 
type_com = ['Type', 'clam shell', '360\u00b0']
mycombobox = ttk.Combobox(root, values = type_com)
mycombobox.current(0)
mycombobox.bind('<<ComboboxSelected>>',selected1)
mycombobox.pack(side = TOP, pady=1) 
recc1 = selected1

#  === WLAN1 position (second question: where is the WLAN1 antenna position)===

label_b = Label(root, text="2. WLAN1 antenna position")
label_b.pack(side = TOP, pady=1) 
WLAN1_ant = ['WLAN1 ant. position', 'hinge', 'top']
mycombobox1 = ttk.Combobox(root, values = WLAN1_ant)
mycombobox1.current(0)
mycombobox1.bind('<<ComboboxSelected>>',selected2)
mycombobox1.pack(side = TOP, pady=1) 
recc2 = selected1

#  === WLAN2 position (third question: where is the WLAN2 antenna position) ===

label_c = Label(root, text="3. WLAN2 antenna position")
label_c.pack(side = TOP, pady=1) 
WLAN2_ant = ['WLAN2 ant. position', 'hinge', 'top']
mycombobox2 = ttk.Combobox(root, values = WLAN2_ant)
mycombobox2.current(0)
mycombobox2.bind('<<ComboboxSelected>>',selected3)
mycombobox2.pack(side = TOP, pady=1) 
recc3 = selected3

# === Result (After choosing answers, push button, and then codes will do calculation)===

mybutton = Button(root, text = 'OK, send out', command = set_intersection(recc1,recc2,recc3))  # <----------------------
mybutton.pack(side = TOP, pady=10)


root.mainloop()

当我运行代码时,它显示错误:

TypeError: 'function' object is not iterable
--> 55     recc = set.intersection(*map(set,collect_all))
--> 40 mybutton = Button(root, text = 'OK, send out', command = set_intersection(recc1,recc2,recc3))

我想我应该在selectedi()中放一个参数,i=1~3,函数类似

recc1 = selected1(values)
recc2 = selected2(values)
recc3 = selected3(values)

但是,值已在组合框中使用。我不知道如何更正我的代码。
感谢任何有用的建议。

我看到的唯一问题是您将整个函数调用转储到 command 参数中。它不是这样工作的。 command 应该等于对要调用的函数的引用,如下所示:

command=set_intersection

但是,您有一堆要传递的参数。您可以使这些参数 global 或者您可以将整个内容包装在 lambda 中。一种方法是这样的:

command = lambda r1=recc1, r2=recc2, r3=recc3: set_intersection(r1,r2,r3)

当你这样做时:command = set_intersection(recc1,recc2,recc3) 你是在告诉 python command 等于 my_list(即来自 set_intersection 的 return功能)。 command 应该等于一个函数,而不是 list,这就是为什么它告诉你“函数对象不可迭代”。它的意思(用更清晰的术语)是:“你不能在我期望函数的地方分配一个列表”

set_intersection(recc1,recc2,recc3) 此代码是 executed when declared 和 return 的列表。列表不能是按钮的命令。按照link解决这个问题。

此外,selected1 到 3 将 return 未定义的值。不管怎样,你需要做什么。

首先将位置参数更改为 selected1-3 的可选关键字参数。这使您可以在没有事件对象的情况下调用这些函数,作为替代方案,您可以传递 None.

def selected1(event=None):   # Type 

下一步是您想要 returned 值而不是存储函数,因此执行它们:

collect_all = [xx1(),xx2(),xx3()]

您还需要使用正确的组合框来获得正确的值:

if mycombobox1.get() == 'WLAN1 ant. position':

虽然我不知道这一行的目的set.intersection(*map(set,collect_all))我会留给你解决

#import openpyxl
#import numpy as np
from tkinter import *
import tkinter.ttk as ttk
#from openpyxl import Workbook  # or "from openpyxl import Workbook": create workbook
#from openpyxl import load_workbook # open/excess excel file or spreadsheet existed
#from IPython.display import clear_output

# The following defines the function for each question shown on screen. 

def selected1(event=None):   # Type 
    if mycombobox.get() == 'Type':
        return 'hello'#Vall
    if mycombobox.get() == 'clam shell':
        return Vcs
    if mycombobox.get() == '360\u00b0':
        return V360

def selected2(event=None):   # WLAN1
    if mycombobox1.get() == 'WLAN1 ant. position':
        return 'world'#Vall
    if mycombobox1.get() == 'hinge':
        return Vhinge1
    if mycombobox1.get() == 'top':
        return Vtop1

def selected3(event=None):    # WLAN2
    if mycombobox2.get() == 'WLAN2 ant. position':
        return 'and all other'#Vall
    if mycombobox2.get() == 'hinge':
        return Vhinge2
    if mycombobox2.get() == 'top':
        return Vtop2

# The following function does the intersection of three sets obtained above. 

def set_intersection(xx1,xx2,xx3):
    collect_all = [xx1,xx2,xx3]
    print(collect_all)
    recc = set.intersection(*map(set,collect_all))   # <----------------------
    my_list = list(recc)
    my_list.sort()
    return my_list

# ================== The main part: GUI. ====================== 

root = Tk()
root.title('Industry Solution')
root.geometry("500x800")

#  === Type (first question: type of computer), three choices: Type, clam shell... ===

label_a = Label(root, text="1. type of computer")
label_a.pack(side = TOP, pady=1) 
type_com = ['Type', 'clam shell', '360\u00b0']
mycombobox = ttk.Combobox(root, values = type_com)
mycombobox.current(0)
mycombobox.bind('<<ComboboxSelected>>',selected1)
mycombobox.pack(side = TOP, pady=1) 
recc1 = selected1

#  === WLAN1 position (second question: where is the WLAN1 antenna position)===

label_b = Label(root, text="2. WLAN1 antenna position")
label_b.pack(side = TOP, pady=1) 
WLAN1_ant = ['WLAN1 ant. position', 'hinge', 'top']
mycombobox1 = ttk.Combobox(root, values = WLAN1_ant)
mycombobox1.current(0)
mycombobox1.bind('<<ComboboxSelected>>',selected2)
mycombobox1.pack(side = TOP, pady=1) 
recc2 = selected2

#  === WLAN2 position (third question: where is the WLAN2 antenna position) ===

label_c = Label(root, text="3. WLAN2 antenna position")
label_c.pack(side = TOP, pady=1) 
WLAN2_ant = ['WLAN2 ant. position', 'hinge', 'top']
mycombobox2 = ttk.Combobox(root, values = WLAN2_ant)
mycombobox2.current(0)
mycombobox2.bind('<<ComboboxSelected>>',selected3)
mycombobox2.pack(side = TOP, pady=1) 
recc3 = selected3

# === Result (After choosing answers, push button, and then codes will do calculation)===

mybutton = Button(root, text = 'OK, send out', command = lambda:set_intersection(recc1(),recc2(),recc3()))  # <----------------------
mybutton.pack(side = TOP, pady=10)


root.mainloop()