简体   繁体   English

如何在不使用 Tkinter 中的条目小部件的情况下制作一个按钮,当按下它时将产品名称显示到文本小部件

[英]how can i make a button that when pressed it shows the name of the product to a Text widget without using an Entry widget in Tkinter

Im trying to make a POS for a burger shop that when i press a button which is the product it displays the name of the product to receipt area which is Text widget without using a Entry widget我试图为一家汉堡店制作一个 POS,当我按下一个产品按钮时,它会在不使用条目小部件的情况下将产品名称显示到收货区域,这是文本小部件

here my sample code这是我的示例代码

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()

thank you谢谢你

sorry for a simple question i'm just a beginner对不起一个简单的问题我只是一个初学者

First you should pass the product to receipt() function as you may have more than one product to be added into the text box.首先,您应该将产品传递给receipt() function,因为您可能有多个产品要添加到文本框中。

Second suggest to use Listbox instead of Text :第二个建议使用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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM