Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Button 让我的按钮向列表中添加文本_Button_User Interface_Python 3.x_Tkinter - Fatal编程技术网

Button 让我的按钮向列表中添加文本

Button 让我的按钮向列表中添加文本,button,user-interface,python-3.x,tkinter,Button,User Interface,Python 3.x,Tkinter,我在让我的按钮实际执行某些操作时遇到问题,当左上角的一个按钮被单击时,我希望在它下面的框架中有一个文本框,将该按钮的名称添加到列表中,但我真的被它卡住了,任何帮助都将不胜感激,谢谢 这是我的密码: import tkinter import tkinter as tk root = tk.Tk() root.geometry('1000x600') var=tk.StringVar() Frame1 = tk.Frame(root) Frame1.configure(background=

我在让我的按钮实际执行某些操作时遇到问题,当左上角的一个按钮被单击时,我希望在它下面的框架中有一个文本框,将该按钮的名称添加到列表中,但我真的被它卡住了,任何帮助都将不胜感激,谢谢

这是我的密码:

import tkinter
import tkinter as tk

root = tk.Tk()
root.geometry('1000x600')

var=tk.StringVar()

Frame1 = tk.Frame(root)
Frame1.configure(background='light blue',height='300',width='500')
Frame1.grid(sticky="nsew",row='0',column='0')

Frame2 = tk.Frame(root)
Frame2.configure(background='grey',height='300',width='500')
Frame2.grid(sticky="nsew",row='0',column='1')

Frame3 = tk.Frame(root)
Frame3.configure(background='grey',height='300',width='500')
Frame3.grid(sticky="nsew",row='1',column='0')

Frame4 = tk.Frame(root)
Frame4.configure(background='light blue',height='300',width='500')
Frame4.grid(sticky="nsew",row='1',column='1')



def PrintOrder():
    LabelOrder = tk.Label(Frame3,text="DONUT ORDER")
    LabelOrder.grid(row='0',column='0')
    return

Button1 = tk.Button(Frame1,text="Apple Cinnamon",height='2',width='15',padx=10, pady=5,command=PrintOrder).grid(row='0',column='0')
Button2 = tk.Button(Frame1,text="Strawberry",height='2',width='15',padx=10, pady=5,command=PrintOrder).grid(row='0',column='2')
Button3 = tk.Button(Frame1,text="Custard",height='2',width='15',padx=10, pady=5,command=PrintOrder).grid(row='0',column='4')
Button4 = tk.Button(Frame1,text="Sugar Ring",height='2',width='15',padx=10, pady=5,command=PrintOrder).grid(row='2',column='0')
Button5 = tk.Button(Frame1,text="Chocolate Caramel",height='2',width='15',padx=10, pady=5,command=PrintOrder).grid(row='2',column='2')
Button6 = tk.Button(Frame1,text="Lemon Circle",height='2',width='15',padx=10, pady=5,command=PrintOrder).grid(row='2',column='4')
Button7 = tk.Button(Frame1,text="Blueberry Blaster",height='2',width='15',padx=10, pady=5,command=PrintOrder).grid(row='4',column='0')
Button8 = tk.Button(Frame1,text="Strawberry Surprise",height='2',width='15',padx=10, pady=5,command=PrintOrder).grid(row='4',column='2')
Button9 = tk.Button(Frame1,text="Simple Sugar",height='2',width='15',padx=10, pady=5,command=PrintOrder).grid(row='4',column='4')

Label1 = tk.Label(Frame2,text="Donut special 6 for the price of 5",height='5',width='30').grid(row='0',column='0')
Button10 = tk.Button(Frame2,text="SPECIAL",padx=5, pady=5,height='5',width='20').grid(row='2',column='0')

Button11 = tk.Button(Frame3,text="ORDER TOTAL",padx=5, pady=5,height='5',width='20').grid(row='2',column='0')


Button12 = tk.Button(Frame4,text="RUNNING TOTAL",padx=5, pady=5,height='5',width='20').grid(row='2',column='0')

root.mainloop()

问题是,当您调用
PrintOrder
函数时,它不一定知道按钮调用了什么

要解决此问题,请使用
lambda
:[请参阅,“向回调传递参数”]

此外,您还需要变量字符串来处理列表

在您的
PrintOrder
函数上方,写下以下内容(将标签创建移到函数之外):

然后可以更新标签中的文本

因此,将
PrintOrder
函数更改为

def PrintOrder(flavor):
    LabelContents.set(LabelContents.get() + '\n' + flavor)
    return
这会在标签上添加一行新的味道

然后在按钮中:

 Button(text="lemon", command=lambda: PrintOrder("lemon"))
当然,您可以修改以适应您的程序

有关lambda背后的原因,请参见链接


希望有帮助

这是一个很大的帮助,非常感谢,还有一些问题,但我会解决的:)
 Button(text="lemon", command=lambda: PrintOrder("lemon"))