如何访问由循环创建的输入框的更改值[Tkinter,Python3.7]

2024-10-03 00:16:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用空闲来发展。在我的程序中,我使用字典和循环来创建输入框。我希望用户能够编辑和保存在这些框中所做的更改。我似乎无法访问任何框中的特定更改条目,因为它们是由循环创建的。我发现与我的问题最接近的是这篇文章:I can't seem to return a specific value from a list created by a loop of Entry boxes.但是,它没有解决我的问题,即在更改后仍然在框中时访问值

下面是我制作盒子的代码:

import tkinter as tk
import sys
import pickle
from tkinter import messagebox

#permanent maps
wood_floors = {'''Red Oak''': 4.27} 
tile_floors = {'''Granite Tile''': 1.28}
laminate_floors = {'''Spruce Laminate''':4.21}
vinyl_floors = {'''White Oak Vinyl''': 2.36}
carpet_floors = {'''Red Carpet''': 3.18}
linearly_priced_items = {'''Quarter Round''': 1.50}
miscelaneous_persqft = {'''Plastic Sheets''': 0.25}
miscelaneous = {'''Nails''': 4.00}
master_list = [wood_floors, tile_floors, laminate_floors, vinyl_floors, carpet_floors, miscelaneous_persqft, miscelaneous]

#temporary maps
temp_wood_floors = {} 
temp_tile_floors = {}
temp_laminate_floors = {}
temp_vinyl_floors = {}
temp_carpet_floors = {}
temp_linearly_priced_items = {}
temp_miscelaneous_persqft = {}
temp_miscelaneous = {}
temp_master_list = [temp_wood_floors, temp_tile_floors, temp_laminate_floors, temp_vinyl_floors, temp_carpet_floors, temp_miscelaneous_persqft, temp_miscelaneous]

def price_editor():
    r = 0
    editorWindow = tk.Tk()
    tk.Label(editorWindow, text = 'WOOD FLOORS').grid(row = r, column = 0)
    for wfloor in wood_floors:
            r = r+1
            b1 = tk.Entry(editorWindow, width = 60)
            b1.insert(10,wfloor)
            b1.grid(row = r, column = 0)
            tk.Label(editorWindow, text = 'Retail Price Per Square Foot: $').grid(row = r, column = 1)
            b2 = tk.Entry(editorWindow)
            b2.insert(10,wood_floors[wfloor])
            b2.grid(row = r, column = 2)
            temp_wood_floors[wfloor] = wood_floors[wfloor]
    r = r+1
    tk.Label(editorWindow, text = ' ').grid(row = r, column= 0)
    r = r+1
    tk.Label(editorWindow, text = 'TILE FLOORS').grid(row = r, column = 0)
    for tfloor in tile_floors:
            r = r+1
            b1 = tk.Entry(editorWindow, width = 60)
            b1.insert(10,tfloor)
            b1.grid(row = r, column = 0)
            tk.Label(editorWindow, text = 'Retail Price Per Square Foot: $').grid(row = r, column = 1)
            b2 = tk.Entry(editorWindow)
            b2.insert(10,tile_floors[tfloor])
            b2.grid(row = r, column = 2)
            temp_tile_floors[tfloor] = tile_floors[tfloor]
    r = r+1
    tk.Label(editorWindow, text = ' ').grid(row = r, column= 0)
    r = r+1
    tk.Label(editorWindow, text = 'LAMINATE FLOORS').grid(row = r, column = 0)
    for lfloor in laminate_floors:
            r = r+1
            b1 = tk.Entry(editorWindow, width = 60)
            b1.insert(10,lfloor)
            b1.grid(row = r, column = 0)
            tk.Label(editorWindow, text = 'Retail Price Per Square Foot: $').grid(row = r, column = 1)
            b2 = tk.Entry(editorWindow)
            b2.insert(10,laminate_floors[lfloor])
            b2.grid(row = r, column = 2)
            temp_laminate_floors[lfloor] = laminate_floors[lfloor]
    r = r+1
    tk.Label(editorWindow, text = ' ').grid(row = r, column= 0)
    r = r+1
    tk.Label(editorWindow, text = 'VINYL FLOORS').grid(row = r, column = 0)
    for vfloor in vinyl_floors:
            r = r+1
            b1 = tk.Entry(editorWindow, width = 60)
            b1.insert(10,vfloor)
            b1.grid(row = r, column = 0)
            tk.Label(editorWindow, text = 'Retail Price Per Square Foot: $').grid(row = r, column = 1)
            b2 = tk.Entry(editorWindow)
            b2.insert(10,vinyl_floors[vfloor])
            b2.grid(row = r, column = 2)
            temp_vinyl_floors[vfloor] = vinyl_floors[vfloor]
    r = r+1
    tk.Label(editorWindow, text = ' ').grid(row = r, column= 0)
    r = r+1
    tk.Label(editorWindow, text = 'CARPET FLOORS').grid(row = r, column = 0)
    for cfloor in carpet_floors:
            r = r+1
            b1 = tk.Entry(editorWindow, width = 60)
            b1.insert(10,cfloor)
            b1.grid(row = r, column = 0)
            tk.Label(editorWindow, text = 'Retail Price Per Square Foot: $').grid(row = r, column = 1)
            b2 = tk.Entry(editorWindow)
            b2.insert(10,carpet_floors[cfloor])
            b2.grid(row = r, column = 2)
            temp_carpet_floors[cfloor] = carpet_floors[cfloor]
    r = r+1
    tk.Label(editorWindow, text = ' ').grid(row = r, column= 0)
    r = r+1
    tk.Label(editorWindow, text = 'LINEARLY PRICED').grid(row = r, column = 0)
    for line in linearly_priced_items:
            r = r+1
            b1 = tk.Entry(editorWindow, width = 60)
            b1.insert(10,line)
            b1.grid(row = r, column = 0)
            tk.Label(editorWindow, text = '            Retail Price Per Foot: $').grid(row = r, column = 1)
            b2 = tk.Entry(editorWindow)
            b2.insert(10,linearly_priced_items[line])
            b2.grid(row = r, column = 2)
            temp_linearly_priced_items[line] = linearly_priced_items[line]
    r = r+1
    tk.Label(editorWindow, text = ' ').grid(row = r, column= 0)
    r = r+1
    tk.Label(editorWindow, text = 'PER SQUARE FOOT ITEMS').grid(row = r, column = 0)
    for miscsqft in miscelaneous_persqft:
            r = r+1
            b1 = tk.Entry(editorWindow, width = 60)
            b1.insert(10,miscsqft)
            b1.grid(row = r, column = 0)
            tk.Label(editorWindow, text = 'Retail Price Per Square foot: $').grid(row = r, column = 1)
            b2 = tk.Entry(editorWindow)
            b2.insert(10,miscelaneous_persqft[miscsqft])
            b2.grid(row = r, column = 2)
            temp_miscelaneous_persqft[miscsqft] = miscelaneous_persqft[miscsqft]
    r = r+1
    tk.Label(editorWindow, text = ' ').grid(row = r, column= 0)
    r = r+1
    tk.Label(editorWindow, text = 'OTHER').grid(row = r, column = 0)
    for thing in miscelaneous:
            r = r+1
            b1 = tk.Entry(editorWindow, width = 60)
            b1.insert(10,thing)
            b1.grid(row = r, column = 0)
            tk.Label(editorWindow, text = '                          Retail Price: $').grid(row = r, column = 1)
            b2 = tk.Entry(editorWindow)
            b2.insert(10,miscelaneous[thing])
            b2.grid(row = r, column = 2)
            temp_miscelaneous[thing] = miscelaneous[thing]

    tk.Label(editorWindow, text = ' ').grid(row = r+1, column = 0)
    tk.Label(editorWindow, text = ' ').grid(row = r+2, column = 0)
    savebutton = tk.Button(editorWindow, text = 'Save', command = save_dialogue).grid(row = r+3, column = 0)
    addfloorbutton = tk.Button(editorWindow, text = 'Add Item', command = add_item).grid(row = r+3, column = 1)
    exitbutton = tk.Button(editorWindow, text = 'Exit Editor', command = exit_dialogue).grid(row = r+3, column = 3)
    removefloorbutton = tk.Button(editorWindow, text = 'Remove Item', command = remove_item).grid(row = r+3, column = 2)

如果我能够在更改框中的值之后访问这些值,那么我就可以对其执行所有其他需要执行的操作。感谢您抽出时间回答我的问题并阅读我的代码。我很抱歉,如果这是一个重复,但我做了彻底的研究之前,问这个问题


Tags: textcolumnb2templabeltkb1grid