Reputation: 43
I am trying to make a program that tells whether a given person's age is smoking age. I was able to make it work in the command line but I decided I wanted to make an actual windowed program. http://pastebin.com/0HettMLx is my current code.
import random
import sys
import os
import time
import tkinter
from tkinter import messagebox, Label, Button, StringVar
age=StringVar
window = tkinter. Tk()#creates a new window
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():
button_pressed=True
while True:
if (age) >= 18:
print('You are legally able to smoke.')
else:
print("You are not of legal age to smoke.")
if (age)>= 18:
print ("You are legally able to smoke cigarettes.")
if (age)>=21:
print("You are legally able to smoke marijuana.")
if (age)>=40:
print("You're above the age of forty,\nDo you really need to ask if you're old enough?")
if (age)<=12:
print("You're to young to smoke get out of here.")
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
But every time I go to run it the window opens but immediately closes. It worked fine with just displaying the button, label, and entry but once I started trying to implement the command=callback into the button it doesn't work. I just want to know how to fix it closing.
Running python34, win7-64bit.
Upvotes: 2
Views: 2043
Reputation: 111
I modify code, and add a comment "modify" identifying show my changed code. see it:
import random
import sys
import os
import time
import tkinter
from tkinter import messagebox, Label, Button, StringVar
window = tkinter.Tk() # creates a new window
window.title("Are you old enough to smoke?") # title
window.geometry("300x200") # window size
# modify: comment it,because no ico on my computer
# window.wm_iconbitmap('favicon.ico') # icon
# photo = tkinter.PhotoImage(file="images.png") # picture in said window
w = tkinter.Label(window)
w.pack()
lbl = tkinter.Label(window, text="Please enter your age.", bg="light salmon", fg="blue2") # label text & color
lbl.pack()
# modify: del textvariable method, use get() method to catch text
ent = tkinter.Entry(window, text="(Your age here)")
ent.pack()
def callback():
# modify: use get() to get text,
age = ent.get()
# modify: check all is numbers
if str(age).isdigit():
age = int(age)
if age <= 12:
print("You're to young to smoke get out of here.")
elif (age) >= 40:
print("You're above the age of forty,\nDo you really need to ask if you're old enough?")
elif (age) >= 21:
print("You are legally able to smoke marijuana.")
elif (age) >= 18:
print("You are legally able to smoke cigarettes.")
else:
print('the age is not number')
#modify: callback() change to callback
btn = tkinter.Button(window, text="Confirm", bg="sienna1", fg="blue2", relief="groove", command=callback)
btn.pack()
window.configure(background='light salmon')
window.mainloop()
Upvotes: 1
Reputation: 5289
When you assign a callback to a button, you need to assign the callable. command=callback()
is assigning what callback()
returns (None
) to the command, instead set command=callback
:
btn=tkinter.Button(window, text="Confirm", bg="sienna1", fg="blue2", relief="groove", command=callback)
Your program is probably crashing because you're trying to access age
by just simply referencing it like a normal variable, StringVars are a little different. To get the value of a StringVar you need to call get()
on it. You'll also need to properly initialize the StringVar, using StringVar()
instead of just StringVar
. You also need to do this after the call to Tk()
:
window = tkinter.Tk() # creates a new window
age = StringVar() # you need to call this after the line above
Finally, in your callback you need to properly access the value of the StringVar and convert it to an integer, this is easiest done at the top of the callback by defining a new variable to hold the integer and then replacing age
. There also is no need to have a while True:
loop in this callback, especially since you never break
out of it:
def callback():
ageint = int(age.get())
button_pressed=True
# Remove while True: loop and fix indentation below
if (ageint) >= 18: # Replace all "age" with "ageint" from here on
print('You are legally able to smoke.')
....
Upvotes: 2