But for many Chicagoans, the gathering had a cathartic, festive feel, with a brass band playing, children dancing and many in the crowd chanting in praise of their city. (“What city protects democracy? Chicago!”)

Ian Wallace, a 38-year-old stay-at-home parent from the Old Town neighborhood whose children attend public elementary school, said he was drawn to the protest to stand against Mr. Trump, his policies and his frequent attacks on Chicago.

“We wanted to show our disappointment with him,” Mr. Wallace said beside three of his children . “There’s a lot of negative things you hear about Chicago, and it’s almost always from someone who doesn’t live here. We love our city.”

Mr. Trump in recent weeks has been showing up in places where he is unpopular, including the San Francisco Bay Area and Minneapolis, and in his brief day trip to Chicago on Monday, he mostly avoided its residents in favor of out-of-town conference attendees.

Last week, Mr. Johnson, the superintendent, said he did not plan to attend Mr. Trump’s speech to police leaders because it “doesn’t line up with our city’s core values, along with my personal values.”

“I can’t in good conscience stand by while racial insults and hatred are cast from the Oval Office, or Chicago is held hostage because of our views on New Americans,” he said in a statement.

Speaking to the International Association of Chiefs of Police, Mr. Trump questioned Mr. Johnson’s values and said he protected illegal immigrants in a violent city.