Python 3.3和TKinter,错误消息和崩溃

时间:2016-02-06 20:20:22

标签: tkinter python-3.3

作为一个爱好,我喜欢涉足这个Python的东西。我正在尝试制作一个简单的程序,帮助我心爱的妻子追踪和识别食物的敏感性。我有一个已经到位的工作版本,但出于无聊我试图通过在主屏幕上添加一个小的自动收报机小部件来改进它。在这样做的时候,我发现了一个我无法弄清楚的错误。我甚至不确定它想要诚实地告诉我什么。但只要我点击"下一步"就会发生这种情况。主菜单中的按钮。

我的代码:

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 = 2)
        self.button_frame.pack()

        self.run_ticker()

    def run_ticker(self):
        ticker_text = self.ticker[self.start:self.end]
        self.ticker_label.config(text = ticker_text, font = ('courier', 10, 'bold'))
        self.start += 1
        self.end += 1
        if self.start == len(self.ticker):
            self.start = 0
            self.end = 29
        self.master.after(250, self.run_ticker)

    def next_win(self):
        for child in self.master_frame.winfo_children():
            child.destroy()
        self.next_frame = Frame(self.master_frame)
        new_label = Label(self.next_frame, text = 'Blah...').grid()
        Button(self.next_frame, text = 'Main', command = self.home_frame).grid()
        self.next_frame.pack()




x = Food('Grapes', 50)
y = Food('Cheese', 75)
z = Food('Eggs', 35)

catalog = {}

catalog[x.name] = x
catalog[y.name] = y
catalog[z.name] = z



root = Tk()
Main(root)

root.mainloop()

我收到的错误消息,对于任一按钮都相同:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python33\lib\tkinter\__init__.py", line 1442, in __call__
    return self.func(*args)
  File "C:\Python33\lib\tkinter\__init__.py", line 501, in callit
    func(*args)
  File "C:\Users\Gregory\Desktop\Test2 Working except ticker.pyw", line 111, in run_ticker
    self.ticker_label.config(text = ticker_text, font = ('courier', 10, 'bold'))
  File "C:\Python33\lib\tkinter\__init__.py", line 1230, in configure
    return self._configure('configure', cnf, kw)
  File "C:\Python33\lib\tkinter\__init__.py", line 1221, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".52922632.52924032"

先生。奥克利在回答我之前的一个问题时非常雄辩地说,“除非你知道引擎盖下发生的事情......”#34;我显然不这样做,这就是为什么我不知道shell试图告诉我的原因。

另外,一个不太重要的问题: 我试图将程序的主菜单保留在屏幕的右上角。我的尝试失败了。它从右上角开始初始化,然后在打开一个新窗口时它正常移动,然后最终返回主菜单后,它会落到正确的位置。显然我也在这里遗漏了一些东西。

一如既往地谢谢!

1 个答案:

答案 0 :(得分:0)

你有一个破坏

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

包含ticker_frame及其所有小部件,因此run_ticker()中的after语句在帧被销毁后仍在调用该函数。创建框架时使用像self.is_tinker_frame = True这样的变量,并在框架被销毁时将其设置为False:

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

此外,在变量设置为False后等待超过250,以销毁帧,以允许任何调用完成。