Python - "'funkce' objekt není iterable " v GUI Combobox s více funkcemi případě

0

Otázka

Účelem kodexů je

  1. Pro každou ze tří otázek (GUI rozhraní s Combobox style), musím si vybrat jednu položku.
  2. Každou položku na odpovídající otázku představuje sadu čísel.
  3. Na základě toho, co jsem si vybrala na tři otázky, kódy, bude výpočet průsečíku nastaví a ukáže odpověď na displeji.

Vall, Vcs, V360...jsou vektory celých čísel. Předpokládejme, že jsou dány. Ex: Vall = [6,8,13,17,23,38]... samozřejmě, že všechny vektory mají stejný rozměr.

Kódy:

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()

Když jsem běžel kódy, to ukazuje chybu:

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))

Myslím, že jsem to měl dát jako parametr v selectedi(), i=1~3, funkce jako

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

Nicméně, hodnoty byla použita v Combobox. Nemám ponětí, jak opravit můj kódy.
Jakékoli užitečné návrhy jsou vítány.

combobox function python tkinter
2021-11-18 14:17:55
2

Nejlepší odpověď

2

set_intersection(recc1,recc2,recc3) tento kód je proveden, když prohlásil, a vrátí seznam. Seznamy nelze příkazového tlačítka. Následovat odkaz, aby tento problém vyřešit.

Kromě toho selected1 až 3 vrátí hodnoty, které nejsou definovány. Každopádně to, co musíte udělat.

První změna poziční argument volitelné klíčové slovo argument pro selected1-3. To dělá abel volat tyto funkce bez objektu události, jako alternativu můžete projít Nic.

def selected1(event=None):   # Type 

Další věc je, že chcete vrácené hodnoty nejsou uloženy funkce, takže jejich vyřízení:

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

Také je potřeba používat správné comboboxes, jak získat správné hodnoty:

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

Když jsem dont vědět, účelem této linky set.intersection(*map(set,collect_all)) Nechám to na vás, abyste vyřešit

#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()
2021-11-18 15:06:09
1

Jediný problém vidím, je, že jste dumpingové celé volání funkce v command argument. To není, jak to funguje. command by se měla rovnat odkaz na funkce, které chcete volat, jako je tento:

command=set_intersection

Nicméně, budete mít spoustu args, které chcete předat. Můžeš těch args global nebo můžete prostě zabalit celou věc v lambda. Jeden způsob, jak to udělat, je, jako je tento:

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

když to budete dělat: command = set_intersection(recc1,recc2,recc3) říkáte, že python command rovná my_list (tj. návratu z set_intersection funkce). command má stejné funkce, ne lista to je důvod, proč je říká, že "funkce není objekt iterable". Co to říká (jasněji) je: "nemůžete přiřadit seznam, kde očekávám funkci"

2021-11-18 15:22:36

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................