這些代碼的目的是
- 對于三個問題(帶有 Combobox 樣式的 GUI 界面)中的每一個,我必須選擇一項。
- 對應問題的每個專案代表一組數字。
- 根據我為三個問題選擇的內容,代碼將計算集合的交集并在螢屏上顯示答案。
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)
但是,Combobox 中已經使用了值。我不知道如何更正我的代碼。
任何有用的建議表示贊賞。
uj5u.com熱心網友回復:
set_intersection(recc1,recc2,recc3)此代碼在宣告時執行并回傳一個串列。串列不能是一個按鈕的命令。按照鏈接解決此問題。
此外 selected1 到 3 將回傳未定義的值。無論如何你需要做什么。
首先將位置引數更改為 selected1-3 的可選關鍵字引數。這使您可以在沒有事件物件的情況下呼叫這些函式,作為替代,您可以傳遞 None。
def selected1(event=None): # Type
接下來是你想要回傳的值而不是存盤的函式,所以執行它們:
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()
uj5u.com熱心網友回復:
我看到的唯一問題是您在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)你告訴 pythoncommand等于my_list(即從你的set_intersection函式回傳)。command應該等于一個函式,而不是 a list,這就是為什么它告訴你“函式物件不可迭代”。它所說的(用更清晰的術語)是:“你不能在我期望一個函式的地方分配一個串列”
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/360443.html
下一篇:類之間的pythonTkinter呼叫函式獲取TypeError__init__()缺少1個必需的位置引數:'parent'
