Home Ask Login Register

Developers Planet

Your answer is one click away!

Gregory6106 February 2016

Python 3.3 and TKinter, error message and crash

As a hobby I like to dabble in this Python stuff. I am trying to make a simple program that will help my beloved wife track and identify food sensitivities. I have a working version of this already in place, but out of boredom I tried to improve it by adding a little ticker widget to the home screen. Upon doing so, I developed an error that I cannot figure out. I'm not even sure what its trying to tell me honestly. But it occurs whenever I click the "Next" button from the main menu.

My code:

from tkinter import *
import os, shelve


class Food:

    def __init__(self, name, score):
        self.name = name
        self.score = score


class Main:

    def __init__(self, master):
        self.master = master
        self.master_frame = Frame(self.master)
        self.master_frame.pack()
        self.home_frame()

    def home_frame(self):
        for child in self.master_frame.winfo_children():
            child.destroy()    
        self.ticker = ' ' * 29
        self.start = 0
        self.end = 29
        for i in catalog.values():
            self.ticker = self.ticker + str(i.name) + ': ' + str(i.score) + '%' + ' ' * 10
        print(self.ticker)
        self.create_widgets()

    def create_widgets(self):

        self.ticker_frame = Frame(self.master_frame, bd = 5, bg = 'black')
        self.ticker_label = Label(self.ticker_frame, text = '<empty>', fg = 'white', bg = 'black')
        self.ticker_label.grid(row = 0, columnspan = 3, sticky = E + W)
        self.ticker_frame.pack(expand = 1, fill = 'x', anchor = 'center')

        self.button_frame = Frame(self.master_frame, bd = 5, bg = 'black')
        Button(self.button_frame, text = 'Next', width = 10, command = self.next_win).grid(row = 0, column = 0)
        Button(self.button_frame, text = 'N/A', width = 10, command = self.master.destroy).grid(row = 0, column = 1)
        Button(self.button_frame, text = 'Exit', width = 10, command = self.master.destroy).grid(row = 0, column        

Answers


Curly Joe February 2016

You have a destroy

def next_win(self):
    for child in self.master_frame.winfo_children():
        child.destroy()

which contains ticker_frame and all of it's widgets, so the after statement in run_ticker() is still calling the function after the frame has been destroyed. Use some variable like self.is_tinker_frame = True when the frame is created, and set it to False when the frame is destroyed plus:

if self.is_tkinter_frame:
    self.master.after(250, self.run_ticker)

Additionally, wait longer than 250 after the variable is set to False, to destroy the frame, to allow any calls to finish.

Post Status

Asked in February 2016
Viewed 2,886 times
Voted 14
Answered 1 times

Search




Leave an answer


Quote of the day: live life