You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Tkinter中实现点击商品按钮向Text组件显示商品名(无需Entry)

Fixing Your Tkinter Burger Shop POS System

Hey there! Let's get your burger shop POS system working just like you want it. I took a look at your code and spotted a couple small issues that are keeping it from running correctly—let's fix them up step by step.

First, let's go over the problems in your original code:

  • You tried to use Burger as a variable, but it's never defined. What you actually want is the string "Burger" to show up in the receipt.
  • Using txt.insert(0.0, item1) adds the item to the start of the text box every time you click the button. That means new orders will appear above old ones, which isn't how a receipt works—we want to add items to the end instead.
  • Right now, multiple clicks would stack the text without line breaks, making the receipt hard to read.

Here's the corrected version of your code:

from tkinter import *

root = Tk()
root.title('Burger Shop POS')
root.geometry('759x500+300+180')
root.resizable(0,0)

def receipt(item):
    # Insert the item at the END of the text box, with a newline for readability
    txt.insert(END, f"{item}\n")

# Button for Burger - use lambda to pass the item name to the receipt function
product1 = Button(root, text="Burger", borderwidth=2, padx=50, pady=40, 
                  command=lambda: receipt("Burger"))
product1.pack()

# Let's add a couple more items to show how scalable this is
product2 = Button(root, text="Fries", borderwidth=2, padx=50, pady=40, 
                  command=lambda: receipt("Fries - $3.99"))
product2.pack()

product3 = Button(root, text="Soda", borderwidth=2, padx=50, pady=40, 
                  command=lambda: receipt("Soda - $1.99"))
product3.pack()

txt = Text(root, width=30, height=20)
txt.pack()

root.mainloop()

Key improvements explained:

  • Lambda functions: These let us pass specific item names directly to the receipt function without writing a separate function for each button. Super handy when you have multiple menu items!
  • END constant: Tkinter's built-in END tells the text box to add new content right after the last character that's already there—perfect for building a receipt in order.
  • Line breaks: Adding \n after each item makes the receipt look clean and organized, just like a real POS receipt.
  • Descriptive item text: I added prices to the fries and soda to show how you can include more details in the receipt if you want.

Bonus tip:

If you want to add a "Clear Receipt" button later, you can make a simple function like this:

def clear_receipt():
    txt.delete(1.0, END)

clear_btn = Button(root, text="Clear Receipt", padx=20, pady=10, command=clear_receipt)
clear_btn.pack()

That's it! Now when you click any of the menu buttons, the item will show up in the receipt text box in the order you clicked them, just like a real POS system.

内容的提问来源于stack exchange,提问作者noobhere

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 19:57:37