如何获得多个组合框的结果



因此,在按下提交按钮后,我试图在组合框中获取所有选定的值。棘手的是,我有一个"添加组合框"按钮,可以为某个部分添加一个新的组合框。我想知道如何从每个组合框中获得选定的值,甚至是添加的组合框。有什么想法吗?

from tkinter import *
from tkinter import messagebox
from tkinter.ttk import Combobox
window = Tk()
window.title('Price Calc')
window.geometry('900x650')
ym = 0
yh = 1
tH = []
cart = []
material = {
'Kydex SC 12x12' : 13.00,
'Kydex SC 8x8' : 10.00,
'Kydex CFB' : 16.00,
'Kydex CG' : 16.00
}
hardware = {
'Eyelets' : 0.15,
'Slotted Post' : 0.75,
'TH Screw' : 0.50,
'Spacer .25' : 0.50,
'Spacer .50' : 0.30,
'Db Spacer' : 0.25,
'Washer' : 0.15,
'Fin Washers' : 0.15
}
mounting = {
'P Paddle S' : 11.00,
'P Paddle L' : 12.00,
'AB Loop 1.50"' : 6.00,
'AB Loop 1.75"' : 7.00,
'Wings 1.75"' : 5.00,
'S Clips 1.75"' : 8.50
}
labor = 30.00
def addm():
global ym, yh
ym += 1
Combobox(window, values=list(material.keys())).grid(column=1, row=ym, pady=10)
ih = h.grid_info()
ihr = ih['row'] + 1
h.grid(sticky=W, column=0, row=ihr, pady=10)
ihl = hl.grid_info()
ihlr = ihl['row'] + 1
hl.grid(column=1, row=ihlr, padx=5)
iah = ah.grid_info()
iahr = iah['row'] + 1
ah.grid(column=2, row=iahr)
yh += 1
def addh():
global yh
global cV
yh += 1
cV = Combobox(window, values=list(hardware.keys()))
cV.grid(column=1, row=yh, pady=10)

def Calculate():
tH.append(hl.get(), cV.get())
print(str(tH))
m = Label(window, text='Material', font=('Arial 11')).grid(sticky=W, column=0, row=0, pady=10)
ml = Combobox(window, state='readonly')
ml['values'] = list(material.keys())
ml.grid(column=1, row=0)
am = Button(window, text='Add', command=addm).grid(column=2, row=0)
h = Label(window, text='Hardware', font=('Arial 11'))
h.grid(sticky=W, column=0, row=1, pady=10)
hl = Combobox(window, state='readonly')
hl['values'] = list(hardware.keys())
hl.grid(column=1, row=1, padx=5)
ah = Button(window, text='Add', command=addh)
ah.grid(column=2, row=1)
c = Button(window, text='Calculate', command=Calculate)
c.grid(column = 3, row=2)
window.mainloop()

还请让我知道我可以做些什么来提高代码的效率。

在制作组合框时将其添加到列表中,然后可以在以后循环使用。我看到你已经有了一个cart变量,这会做得很好。

让我知道我可以做些什么来提高代码的效率。

好吧,既然你问:你也可以制作一个新的Frame来放置组合框。这样你就不必跟踪行了。

from tkinter import *
from tkinter import messagebox
from tkinter.ttk import Combobox
window = Tk()
window.title('Price Calc')
window.geometry('900x650')
material = {
'Kydex SC 12x12' : 13.00,
'Kydex SC 8x8' : 10.00,
'Kydex CFB' : 16.00,
'Kydex CG' : 16.00
}
hardware = {
'Eyelets' : 0.15,
'Slotted Post' : 0.75,
'TH Screw' : 0.50,
'Spacer .25' : 0.50,
'Spacer .50' : 0.30,
'Db Spacer' : 0.25,
'Washer' : 0.15,
'Fin Washers' : 0.15
}
mounting = {
'P Paddle S' : 11.00,
'P Paddle L' : 12.00,
'AB Loop 1.50"' : 6.00,
'AB Loop 1.75"' : 7.00,
'Wings 1.75"' : 5.00,
'S Clips 1.75"' : 8.50
}
labor = 30.00
def addm():
ml = Combobox(material_frame, values=list(material.keys()))
ml.grid(column=1, pady=10)
cart.append(ml)
def addh():
cV = Combobox(hardware_frame, values=list(hardware.keys()))
cV.grid(column=1, pady=10)
cart.append(cV)
def Calculate():
for box in cart:
print(box.get())
cart = []
material_frame = Frame(window)
material_frame.grid(column=1, row=0)
addm() # make the first combobox
Label(material_frame, text='Material', font=('Arial 11')).grid(sticky=W, column=0, row=0, pady=10)
Button(material_frame, text='Add', command=addm).grid(column=2, row=0)
hardware_frame = Frame(window)
hardware_frame.grid(column=1, row=1)
addh()
h = Label(hardware_frame, text='Hardware', font=('Arial 11'))
h.grid(sticky=W, column=0, row=0, pady=10)
ah = Button(hardware_frame, text='Add', command=addh)
ah.grid(column=2, row=0)
c = Button(window, text='Calculate', command=Calculate)
c.grid(column = 3, row=1)
window.mainloop()

相关内容

  • 没有找到相关文章

最新更新