Начиная с простых уроков Tkinter, я застрял в случае, если даже простой код не работает:
import tkinter as tk
root = tk.Tk()
b = tk.Button(root, text='button'); b.pack()
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/../anaconda3/lib/python3.6/tkinter/__init__.py", line 2366, in __init__
Widget.__init__(self, master, 'button', cnf, kw)
File "/Users/../anaconda3/lib/python3.6/tkinter/__init__.py", line 2296, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: can't invoke "button" command: application has been destroyed
И не могу найти причину, учитывая, что этот код взят из официальной документации.
С другой стороны, другой код работает:
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
self.hi_there = tk.Button(self)
self.hi_there["text"] = "Hello World\n(click me)"
self.hi_there["command"] = self.say_hi
self.hi_there.pack(side="top")
self.quit = tk.Button(self, text="QUIT", fg="red",
command=self.master.destroy)
self.quit.pack(side="bottom")
def say_hi(self):
print("hi there, everyone!")
root = tk.Tk()
app = Application(master=root)
app.mainloop()
Я попытался обновить tk
от conda: conda install -c anaconda tk
, но ничего не изменилось. Не могу понять, почему.