python 从Tkinter中数量可变的输入框中获取值

rkue9o1l  于 2023-01-04  发布在  Python
关注(0)|答案(1)|浏览(161)

我已经创建了一个简单的GUI,它在一列中显示来自给定列表的变量作为标签小部件,然后在相邻列中显示相应数量的条目框。我能够"获取"最后一个条目小部件的值,没有任何问题。然而,我希望"获取"所有条目小部件的值。理想情况下,这将是一个字典,但我现在可以接受任何输出,我看过Is there anyway to get all the values of a Entry Widget that I put in a for loop? x一e一f一x x一e二f一x x一e3到x一; x一e4到x一;还有很多很多,但是没有一个能给出一个直接的答案,或者至少一个我能应用的答案。
标签/条目小部件的数量可以从1到15不等,事先无法知道。

import sys
import tkinter as tk
from tkinter import ttk

def share_cost_basis(fund, pool, price):
    def get_px(*args):
        # This is the issue part
        for p in price:
            my_px = float(p) * float(entry.get())
            print(my_px)
        # End issue part
        form.destroy()
        sys.exit(0)
    form = tk.Tk()
    form.update_idletasks()
    width = 400
    height = 450
    x = int((form.winfo_screenwidth() / 2) - (width / 2))
    y = int((form.winfo_screenheight() / 2) - (height / 2))
    form.geometry('{}x{}+{}+{}'.format(width, height, x, y))
    form.title('Cost Basis per Share Entry')
    form_label = ttk.Label(form, text=f'For the fund {pool} {fund}, please enter the number of\n'
                                      f' shares that were purchased at each price.\n')
    form_label.grid(column=1, row=1, columnspan=4, sticky='E,W', pady=(20, 0), padx=(10, 0))
    px = tk.StringVar()
    for p in price:
        px.set(p)
        label_frame = ttk.LabelFrame(form, borderwidth=10, relief='sunken')
        row_num = price.index(p) + 2
        label_frame.grid(row=row_num, column=1, pady=5, sticky='e', columnspan=2)
        label = ttk.Label(label_frame, text=p)
        label.grid(row=row_num, column=1, columnspan=2)
        entry = ttk.Entry(form)
        entry.grid(row=row_num, column=3)
    ok_button = ttk.Button(form, text='OK', command=get_px)
    ok_button.grid(row=row_num+2, column=2)
    form.mainloop()

share_cost_basis('Test Fund', 'Test Pool', ['1.25', '2.50', '3.75', '5.00'])

任何和所有的帮助是感激的。
谢谢!

ee7vknir

ee7vknir1#

因此,对我来说,这种情况下的解决方案是使用一个列表来保存条目字段,然后根据索引检查值。
下面是一个代码修改为使用列表示例。

import sys
import tkinter as tk
from tkinter import ttk

def share_cost_basis(fund, pool, price):
    def get_px(*args):
        for ndex, p in enumerate(price):
            # based on the index of your price list we also pull the value from the entry list
            my_px = float(p) * float(list_of_entry_fields[ndex].get())
            print(my_px)
        form.destroy()
        sys.exit(0)

    form = tk.Tk()
    form.update_idletasks()
    list_of_entry_fields = []  # Added a list to store entry fields
    width = 400
    height = 450
    x = int((form.winfo_screenwidth() / 2) - (width / 2))
    y = int((form.winfo_screenheight() / 2) - (height / 2))
    form.geometry('{}x{}+{}+{}'.format(width, height, x, y))
    form.title('Cost Basis per Share Entry')
    form_label = ttk.Label(form, text=f'For the fund {pool} {fund}, please enter the number of\n'
                                      f' shares that were purchased at each price.\n')
    form_label.grid(column=1, row=1, columnspan=4, sticky='E,W', pady=(20, 0), padx=(10, 0))
    px = tk.StringVar()
    for p in price:
        px.set(p)
        label_frame = ttk.LabelFrame(form, borderwidth=10, relief='sunken')
        row_num = price.index(p) + 2
        label_frame.grid(row=row_num, column=1, pady=5, sticky='e', columnspan=2)
        label = ttk.Label(label_frame, text=p)
        label.grid(row=row_num, column=1, columnspan=2)
        # appending list of entry fields with each one in the loop
        list_of_entry_fields.append(ttk.Entry(form))
        # after appending we can use -1 to reference the last item appened
        list_of_entry_fields[-1].grid(row=row_num, column=3)
    ok_button = ttk.Button(form, text='OK', command=get_px)
    ok_button.grid(row=row_num+2, column=2)
    form.mainloop()

share_cost_basis('Test Fund', 'Test Pool', ['1.25', '2.50', '3.75', '5.00'])

结果:

打印到控制台的值:

2.5
7.5
15.0
25.0

相关问题