在不使用Tkinter中的Entry小部件的情况下,我如何制作一个按钮,当按下它时,它会向Text小部件显示产品名称



我正试图为一家汉堡店制作一个POS,当我按下产品按钮时,它会将产品名称显示到收据区域,该区域是文本窗口小部件,而不使用输入窗口小部件

这是我的样本代码

from tkinter import *
root = Tk()
root.title('Login')
root.geometry('759x500+300+180')
root.resizable(0,0)

def receipt():

item1 = Burger
txt.insert(0.0, item1)

product1 = Button(root, text="Burger", borderwidth=2, padx=50, pady=40, command=receipt)
product1.pack()
txt = Text(root, width=30, height=20)
txt.pack()

root.mainloop()

谢谢

很抱歉问了一个简单的问题,我只是的初学者

首先,您应该将产品传递给receipt()函数,因为您可能有多个产品要添加到文本框中。

第二个建议使用Listbox而不是Text:

from tkinter import *
root = Tk()
root.title('Login')
root.geometry('+300+180')
root.resizable(0,0)

def receipt(item):
lstbox.insert('end', item)
button_frame = Frame(root)
button_frame.pack(side='left', fill='y')
products = ["Burger", "Apple Pie"]
for product in products:
btn = Button(button_frame, text=product, width=15, height=3, borderwidth=2,
command=lambda p=product: receipt(p))
btn.pack()
lstbox = Listbox(root, width=30, height=20, activestyle='none')
lstbox.pack(side='right')

root.mainloop()

相关内容

最新更新