При выводе данных из класса выдаёт ошибки Exception in Tkinter callback и invalid command name ".!w2.!entry"
Делаю графический интерфейс для ввода данных в приложение по расчёту рациона. Пользователь вводит свои характеристики, далее необходимо вывести полученные данные в другой класс, при выводе же выдаёт ошибки:
Traceback (most recent call last):
File "C:\Users\Michael\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "c:\Users\Michael\Desktop\Coding\Python\Курсовая\GUI\GUI-2.py", line 16, in open_window
sex = window2.open()
^^^^^^^^^^^^^^
File "c:\Users\Michael\Desktop\Coding\Python\Курсовая\GUI\GUI-2.py", line 74, in open
sex = sex.get()
^^^
sex = window2.open()
^^^^^^^^^^^^^^ File "c:\Users\Michael\Desktop\Coding\Python\Курсовая\GUI\GUI-2.py", line 75, in open
age = self.age.get()
^^^^^^^^^^^^^^
File "C:\Users\Michael\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 3099, in get
return self.tk.call(self._w, 'get')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_tkinter.TclError: invalid command name ".!w2.!entry"
Сам код:
class W1(Tk):
def __init__(self):
super().__init__()
Label(self, text="Расчёт рациона:").place(x=500, y=300)
Button(self, text="Начать", command=self.open_window).place(x=500, y=500)
def open_window(self):
window2 = W2(self)
sex = window2.open()
age = window2.open()
height = window2.open()
weight = window2.open()
activity = window2.open()
print(sex)
print(age)
print(height)
print(weight)
print(activity)
Button(self, text="Завершить", width=25, command=self.destroy).place(x=150, y=350)
class W2(Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.sex = BooleanVar()
self.age = IntVar()
self.height = DoubleVar()
self.weight = DoubleVar()
self.activity = DoubleVar()
Label(self, text="Введите ваш пол:").place(x=100, y=100)
Label(self, text="Введите ваш возраст:").place(x=100, y=150)
Label(self, text="Введите свой рост (в см)").place(x=100, y=200)
Label(self, text="Введите свой вес (в кг)",).place(x=100, y=250)
Label(self, text="Выберите тип вашей физической активности: ").place(x=400, y=100)
Radiobutton(self, text=" М ", variable=self.sex, value=0).place(x=200, y=100)
Radiobutton(self, text=" Ж ", variable=self.sex, value=1).place(x=250, y=100)
Radiobutton(self, text="Минимальные физические нагрузки", variable=self.activity, value=1.2,).place(x=400, y=150)
Radiobutton(self, text="Небольшая дневная активность или легкие упражнения 1-3 раза в неделю", variable=self.activity, value=1.4,).place(x=400, y=200)
Radiobutton(self, text="Тренировки в фитнес-зале 4-5 раз в неделю или работа средней тяжести", variable=self.activity, value=1.6,).place(x=400, y=250)
Radiobutton(self, text="Интенсивные тренировки 4-5 раз в неделю", variable=self.activity, value=1.8,).place(x=400, y=300)
Radiobutton(self, text="Ежедневные тренировки", variable=self.activity, value=2).place(x=400, y=350)
Radiobutton(self, text="Ежедневные интенсивные тренировки или обычные тренировки 2 раза в день", variable=self.activity, value=2.2,).place(x=400, y=400)
Radiobutton(self, text="Интенсивные тренировки 2 раза в день или тяжелая физическая работа", variable=self.activity, value=2.4,).place(x=400, y=450)
Button(self, text="Продолжить", command=self.destroy).place(x=450, y=600)
self.age = Entry(self)
self.age.place(x=250, y=150)
self.height = Entry(self)
self.height.place(x=250, y=200)
self.weight = Entry(self)
self.weight.place(x=250, y=250)
def open(self):
self.grab_set()
self.wait_window()
sex = self.sex.get()
age = self.age.get()
height = self.height.get()
weight = self.weight.get()
activity = self.activity.get()
return sex, age, height, weight, activity
if __name__ == "__main__":
root = W1()
root.geometry("1000x800")
root.mainloop()
Подскажите, пожалуйста, в чём проблема...