我的tkinter窗口立即关闭

时间:2015-12-10 04:19:38

标签: python python-3.x tkinter

我正在尝试制作一个程序,告诉某个人的年龄是否适合吸烟年龄。我能够在命令行中使它工作,但我决定我想制作一个实际的窗口程序。 http://pastebin.com/0HettMLx是我目前的代码。

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

但是每次我去运行它时窗口都会打开,但会立即关闭。只显示按钮,标签和条目它工作得很好,但是一旦我开始尝试将command = callback实现到按钮中它就不起作用。我只是想知道如何解决这个问题。

运行python34,win7-64bit。

2 个答案:

答案 0 :(得分:1)

为按钮分配回调时,需要指定可调用对象。 command=callback()正在为命令分配callback()返回的内容(None,而不是设置command=callback

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

您的程序可能会崩溃,因为您只是像普通变量一样简单地引用它来尝试访问age,StringVars有点不同。要获取StringVar的值,您需要在其上调用get()。您还需要使用StringVar()而不是StringVar正确初始化StringVar。您还需要在> <{1}} 之后执行此操作

Tk()

最后,在你的回调中你需要正确访问StringVar的值并将其转换为整数,这在回调顶部最容易完成,方法是定义一个新变量来保存整数然后替换{{1 }}。此回调中也不需要window = tkinter.Tk() # creates a new window age = StringVar() # you need to call this after the line above 循环,特别是因为你从不age出现它:

while True:

答案 1 :(得分:1)

我修改了代码,并添加了评论“修改”来识别显示我更改的代码。 看到它:

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