WARSAW — Growing up in a small city in southern Poland, part of a religious family and conservative community, Maciej Gosniowski was told again and again that something was wrong with him.
“It would be better if I changed myself,” he recalled teachers telling him. “It would be better if I behaved more like a boy. It would make my life easier.”
Mr. Gosniowski was beaten by other students who used homophobic slurs he did not yet understand. He does not want other young people to suffer as he did, so he welcomed the decision by the mayor of Warsaw to introduce a declaration last month aimed at promoting tolerance.
But the backlash to the declaration has left him shaken.
Poland’s governing party, Law and Justice, has seized on the declaration and the issue of gay rights in its campaigning for European Union elections in May and for national elections this fall.
Where the party once attacked migrants as a threat to the soul of the country, gay people have become its public enemy No. 1 in recent weeks.
It is part of a growing trend across eastern and central Europe, where nationalist and populist parties are increasingly turning to cultural issues — and attacks on gay people — to rally their faithful.
From Romania, where the government tried and failed to change the Constitution to ban same-sex marriage, to Hungary, where homosexuals are vilified as a threat to traditional families, the letters L.G.B.T. are being scorned as part of a broader struggle against what the nationalists and populists call “European values.”
Jaroslaw Kaczynski, the leader of Law and Justice and the most powerful politician in Poland, used the party’s convention in March to declare that this was a war Poland must win to survive.
“It comes down to, as we know today, sexualization of children from the earliest childhood,” he said. “We need to fight this. We need to defend the Polish family. We need to defend it furiously because it’s a threat to civilization, not just for Poland but for the entire Europe, for the entire civilization that is based on Christianity.”
The party’s rank and file have rallied to the call.
“I think that Poland will be a region free from L.G.B.T.,” said Elzbieta Kruk, who is running on the party’s list for a seat in the European Parliament. “I hope it will be.”
Other groups have echoed the attacks, often in more extreme language.
Fans of one of the country’s most popular soccer clubs, Legia Warsaw, unfurled a banner during a match last month that used a homophobic slur. No action was taken during the match. The club later said that it did not wish to be brought into “political and ideological fights” and that the banner “did not reflect Legia’s views.”
Leading Polish figures in the Roman Catholic Church, itself reeling from revelations about sexual abuse by priests, have also joined in.
The Rev. Marek Dziewiecki, a well-known Catholic priest and educator, told a local radio station in a recent interview that the plus in “L.G.B.T.Q.+” stood for “pedophiles, zoophiles, necrophiles,” and that the ultimate goal was to “make people into infertile erotomaniacs.”
When some 1,500 supporters of far-right groups came to Czestochowa, Poland’s holiest site, this month, the Rev. Henryk Grzadko warned those gathered that Poland was experiencing a “civilizational invasion.”
“It comes down to them waving a rainbow flag and trying to steal our internal values like truth, love, human life, family based on marriage, and morality based on Gospel and Decalogue,” he said during his homily at a Mass for the gathering.
Rafal Trzaskowski, the mayor of Warsaw, who issued the tolerance declaration, said that while he had expected a cynical response from the government, he worried about the sort of propaganda state outlets had produced.
It was the same kind of bile, he said, that led a man to fatally stab the mayor of Gdansk, Pawel Adamowicz, on live television earlier this year.
“They are basing their policy on fear,” he said in an interview at his City Hall office. “They started a few years back with refugees when they painted this abysmal picture that we are going to be overrun by hundreds of thousands of migrants that are going to rape our women and introduce diseases into Poland. They are now doing exactly the same thing.”
But he does not think it will work. In 2015, when Law and Justice came to power, an overwhelming majority of Poles agreed with the idea that more needed to be done to secure Europe’s borders from migrants.
That threat has largely passed, and the issue does not have the resonance it once did, according to a poll released this week by the European Council on Foreign Relations.
“While migration is important for some voters, it is not the only battleground for votes ahead of the European Parliament elections,” the group said in a statement.
Mr. Trzaskowski, the Warsaw mayor, said he did not believe attacking gay people would prove as effective as the campaign against migrants.
“The majority of the Polish people will not buy the idea that homosexuals endanger our culture or values,” he said.
An openly gay candidate, Robert Biedron, formed a new liberal party and has found widespread support for his message, including outside urban centers.
A recent public opinion poll conducted by Ipsos for the news outlet OKO.Press found that 56 percent of Poles did not oppose civil partnerships; two years ago, it was 52 percent.
At the same time, though, Poles remain steadfastly opposed to adoption by gay couples, with only 18 percent support.
Oktawiusz Chrzanowski, 36, who played a key role in helping draft the Warsaw declaration, and his partner, Hubert Sobecki, said one of the most insidious parts of the government campaign was an effort to paint homosexuality as a danger to children.
“The most shocking and disgusting thing is how they can claim people in the L.G.B.T. community are pedophiles,” Mr. Sobecki said.
The Warsaw declaration calls for sex education in schools that follows guidelines set out by the World Health Organization, and for a shelter in the city for people cast out by their family and community.
Mr. Chrzanowski said he hoped that one day soon all schools would have a “lighthouse keeper,” someone students could turn to for advice without fear of judgment.
Mr. Gosniowski, who decided to come out not long after he was beaten in high school and works as one of only a handful of drag performers in the country, said he was now confident in his sexuality.
At lunch recently at a Warsaw cafe, he wore a pink sweatshirt and big gold hoop earrings under his long blond hair — a definite style statement in a country where nonconformity can still come at a cost.
Young people often ask him about coming out safely.
“I feel I am needed here,” he said. “People here are scared to walk hand in hand in the street.”
While the vitriol being directed at gays is alarming, he said, he has also noticed an outpouring of support that surprised him.
“The two wings of society seem to be spreading apart,” he said. “I have found more support than ever. But also people who are more hateful than ever.”