我刚读完一本名为Practical Programming 2nd ed的python书。并且我认为我会挑战自己应用我学到的东西来制作我的第一个小型工作程序(便利店)来创建我需要使用tkinter作为界面订购的芯片列表。 我会输入我库存的每个品牌的芯片中有多少,按下“订购”按钮后,它将返回订购所需的每个品牌芯片的数量。
这是我到目前为止的代码:
import tkinter as tk
# Initiate tkinter
window = tk.Tk()
# Create a dictionary of brand of chips with a value of how many chips should be in stock
chips = {'Miss Vickies Original': 4, 'Miss Vikies Salt & Vinegar': 4}
# Start counting rows
row_num = 0
for brand in chips:
# Create label describing the brand of chips
label = tk.Label(window, text=brand)
label.grid(row=row_num, column=1)
# Create an entry box to input number of chips in stock
entry = tk.Entry(window)
entry.grid(row=row_num, column=2)
# Add one; To start at next row
row_num += 1
# Return number of chips to order
def order(entry_data, stock_required):
# Subtract how much is in stock from how much should be in stock
order_quantity = int(stock_required) - int(entry_data)
# Create label to the left of each chip with how many should be ordered
order_label = tk.Label(window, text="'Order: ', order_quantity")
order_label.pack(side='left')
# Order button
button = tk.Button(window, text='Order', command=lambda: order(entry.get(), chips[1]))
button.pack(side='bottom')
window.mainloop()
我想我已经把自己与自己的代码混淆了。我很难弄清楚如何使按钮(放在底部)触发计算,并显示每个芯片标签左侧的order_label
。
我在代码中看到的问题是我不确定如何修复标签只会被调用一次,因为该函数不包含循环。推荐的解决方法是什么?
我查了一下:Tkinter: Link an Entry Widget to a Button to a Function
但不是相关问题,因为我已经有lambda: order
函数。
以下是它的外观
这是我的第一个项目,所以鼓励任何建设性的批评! 提前致谢
答案 0 :(得分:1)
我添加了使您的代码按预期工作所需的功能;您需要跟踪当前的库存水平,以使其成为有用的东西。
import tkinter as tk
def generate_order_quantities():
for brand, datafields in chips_entry_fields.items():
entry, quantity_to_order, label = datafields
recommended_stock_level = chips_stock_levels[brand]
try:
quantity_sold = int(entry.get())
except ValueError:
quantity_sold = 0
quantity_to_order.set(min(quantity_sold, recommended_stock_level))
if __name__ == '__main__':
# Initiate tkinter
window = tk.Tk()
# Create a dictionary of brand of chips_stock_levels with a value of how many chips_stock_levels should be in stock
chips_stock_levels = {'Miss Vickies Original': 4, 'Miss Vikies Salt & Vinegar': 4}
chips_quantities_sold = {'Miss Vickies Original': 0, 'Miss Vikies Salt & Vinegar': 0}
chips_entry_fields = {}
# Start counting rows
row_num = 0
for row, brand in enumerate(chips_stock_levels):
# Create label describing the brand of chips_stock_levels
label = tk.Label(window, text=brand)
label.grid(row=row, column=1)
# Create an entry box to input number of chips_stock_levels in stock
entry = tk.Entry(window)
chips_entry_fields[brand] = [entry]
entry.grid(row=row, column=2)
# Create label to the left of each chip with how many should be ordered
quantity_to_order = tk.IntVar()
chips_entry_fields[brand].append(quantity_to_order)
# quantity_to_order.set()
order_label = tk.Label(window, textvariable=quantity_to_order)
chips_entry_fields[brand].append(order_label)
order_label.grid(row=row, column=3)
# Order button
button = tk.Button(window, text='Order', command=generate_order_quantities)
button.grid(row=3, column=3)
window.mainloop()