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

Python 需要使tkinter消息框显示基于条件的消息,python,tkinter,python-3.4,Python,Tkinter,Python 3.4,我试图使callback命令中的messagebox将条件的结果显示为消息。所以它就像messagebox.showinfotitle=你会抽烟吗?、message=在这里插入上一个的结果。我试着在信息中加入ageint,但这只是显示了一些随机数 import tkinter from tkinter import messagebox, Label, Button, StringVar window = tkinter. Tk()#creates a new window age=Strin

我试图使callback命令中的messagebox将条件的结果显示为消息。所以它就像messagebox.showinfotitle=你会抽烟吗?、message=在这里插入上一个的结果。我试着在信息中加入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
只需将print替换为msg=即可将字符串分配给引用:

msg = "You are legally able...
然后将消息放入消息中:


你为什么还要制作一个关于吸食大麻的节目!?这个节目不是关于大麻的。基于你的年龄,吸烟只是一个简单的开放式话题。这个课程严格来说是为了练习,因为我试图用教程和其他资源来自学。我不敢相信这段时间竟然那么容易。我真是个笨蛋。非常感谢你!
messagebox.showinfo(title="Can you smoke?", message=msg)