【问题标题】:Getting my buttons to add text to a list让我的按钮将文本添加到列表中
【发布时间】:2014-01-20 10:05:02
【问题描述】:

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

这是我的代码:

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

【问题讨论】:

    标签: button user-interface python-3.x tkinter


    【解决方案1】:

    问题是当你调用PrintOrder函数时,它不一定知道是哪个按钮调用了它。

    要解决此问题,请使用 lambda:[参见 http://effbot.org/zone/tkinter-callbacks.htm,“将参数传递给回调”]

    此外,您还需要使用可变字符串来处理列表。

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

    LabelContents = tk.StringVar()
    LabelOrder = tk.Label(Frame3, textvariable=LabelContents)
    LabelOrder.grid(row='0', column='0')
    

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

    所以把PrintOrder函数改成

    def PrintOrder(flavor):
        LabelContents.set(LabelContents.get() + '\n' + flavor)
        return
    

    这会在标签上添加新的一行。

    然后在按钮中:

     Button(text="lemon", command=lambda: PrintOrder("lemon"))
    

    当然你可以修改以适应你的程序。

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

    希望有帮助!

    【讨论】:

    • 帮助很大,非常感谢,还有一些问题,但我会解决的 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 2017-03-22
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多