需要使tkinter消息框显示基于条件的消息

时间:2015-12-11 04:27:46

标签: python tkinter python-3.4

我试图使回调命令中的消息框显示条件的结果作为消息。所以它就像messagebox.showinfo(title="Can you Smoke?, message=(Insert result of previous here)。我已经尝试将ageint添加到消息中,但这只是向我显示了一些随机数字。

import tkinter
from tkinter import messagebox, Label, Button, StringVar

window = tkinter. Tk()#creates a new window
age=StringVar()
window.title("Are you old enough to smoke?")#title
window.geometry("300x200")#window size
window.wm_iconbitmap('favicon.ico')#icon

photo=tkinter.PhotoImage(file="images.png")#picture in said window
w=tkinter.Label(window, image=photo)
w.pack()

lbl=tkinter.Label(window, text="Please enter your age.", bg="light salmon", fg="blue2")#label text & color
lbl.pack()

ent=tkinter.Entry(window, text="(Your age here)", textvariable=age)
ent.pack()


def callback():
    ageint=int(age.get())
    button_pressed=True
    if ageint >= 18:
        print('You are legally able to smoke.')
    else:
        print("You are not of legal age to smoke.")

    if ageint >= 18:
        print ("You are legally able to smoke cigarettes.")
    if ageint >=21:
        print("You are legally able to smoke marijuana.")
    if ageint >=40:
        print("You're above the age of forty,\nDo you really need to ask if you're old enough?")
    if ageint <=12:
        print("You're to young to smoke get out of here.")

    messagebox.showinfo(title="Can you smoke?",)

btn=tkinter.Button(window, text="Confirm", bg="sienna1", fg="blue2", relief="groove", command=callback)
btn.pack()

window.configure(background='light salmon')#back ground

window.mainloop()# draws window

1 个答案:

答案 0 :(得分:3)

只需将print替换为msg =即可将字符串指定给引用:

msg = "You are legally able...

然后将msg放入message

messagebox.showinfo(title="Can you smoke?", message=msg)