.Работа с классами
После добавления данного кода(•), возникла ошибка. Не понимаю в чём дело...
Ожидается, что класс Button будет корректно работать во всех модулях класса Test.
import pygame
from w import Button #•
from tkinter import *
class Test:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((500,500))
self.s_color =(0,130,130)
self.font = pygame.font.SysFont('Arial', 20)
self.button = Button() #•
def run_game(self):
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
self.screen.fill(self.s_color)
t2 = self.screen.blit(self.font.render('Тест', True, (0, 0, 0)), (450, 600))
pygame.display.flip()
ai = Test()
ai.run_game()
w.py :
from tkinter import *
class Button():
def __init__(self):
self.root = Tk()
self.root_g = root.geometry("300x250")
self.bth = Button(text="Test")
self.bth_p = btn.pack()
self.root_m = root.mainloop()
Ошибка:
Traceback (most recent call last):
File "/storage/emulated/0/pyhpn.v2/for work.py", line 29, in <«module>
ai = Test()
File "/storage/emulated/0/pyhpn.v2/for work.py", line 13, in _init
self.button = Button()
File "/data/user/0/ru.iiec.pydroid3/files/ aarch64-linux-android/lib/python3.8/tkinter/ init.py", line 2655, in init
Widget.init(self, master, ‘button’, cnf, kw)
File "/data/user/0/ru.iiec.pydroid3/files/ aarch64-linux-android/lib/python3.8/tkinter/ init__.py", line 2577, in init
self.tk.call( AttributeError: 'NoneType' object has no attribute ‘call’
Ответы (1 шт):
Проблема заключалась в том, что в w.py, в классе Button(а он одноимённ с классом библиотеки tkinter), я пытался инициализировать класс с тем же именем, что породило ошибку. Я сделал следующее:
/w.py Заменил имя моего класса на Knopka.
/Основной код Также заменил имя класса на Knopka, во второй точке #•
Теперь всё работает