满足条件时出现按钮

时间:2019-07-08 06:09:43

标签: python-3.x tkinter

我是python的新手,我正在尝试tkinter。目前,我正在使用random从列表中进行选择。当选择列表时,我希望某些按钮出现。等等,当选择了Pinoeer Mrt时,Button1出现供我单击,否则,如果选择了其他按钮,则出现相同的按钮但执行另一项功能。

我已向stackoverflow寻求帮助,但似乎找不到与我的需求相关的任何东西。如果您有任何链接,请在下面评论,我会看一下。谢谢!

def lunchLocation():

    location_list = ['Pinoeer Mrt', 'Joo Koon Mrt', 'Avenue 3 - Hot Sun', 'Walk 1km - Hot Sun']

    Location = random.choice(location_list)
    print("Today we eat at ",Location ," lah!")

def foodChoicePinoeer():

    ryan_Pinoeer = ['Minced Meat Noodle', 'Hotplate Chicken', 'fishball Soup']
    elieen_Pinoeer = ['Hotplate Chicken', 'fishballNoodle' , 'banMian']
    tommy_Pinoeer = ['Chicken Rice', 'Hotplate chicken', 'Chai Fan']
    rob_Pinoeer = ['Hotplate Chicken', 'Ban Mian', 'Minced Meat Noodle']

    ryan = random.choice(ryan_Pinoeer)
    print("Today Ryan eat ",ryan_Pinoeer, 'ok?')
    print("/nWhile Elieen eat ",elieen_Pinoeer,'lah !')
    print("/nAnd Tommy will eat ",tommy_Pinoeer,'!!')
    print("/nLastly rob will eat ",rob_Pinoeer," hor")

root = tk.Tk()

canvas1 = tk.Canvas(root, width=500, height=500)
canvas1.pack()

button1 = tk.Button(text='Choose Food', command=foodChoicePinoeer(), bg='brown', fg='white')

canvas1.create_window(300, 150, window=button1)

root.mainloop()

1 个答案:

答案 0 :(得分:0)

您可以简单地将not_selected方法移至create_window函数中。添加一个lunchLocation子句以检查位置是否符合您的要求。

if