title: Python tkinter: main thread is not in main loop?
body: <p>I am developing a GUI for mine MQTT app.
  I have a problem with this function:</p>
  <pre><code>   from tkinter import *
  from tkinter import ttk
  from tkinter import messagebox
  from PIL import  ImageTk, Image
  import paho.mqtt.client as mqtt
  import json
  
  broker = &quot;192.168.1.9&quot;
  client = mqtt.Client(&quot;DoIP_project&quot;)
  m_decode = ''
  
  def on_log(client, userdata, level, buf):
      print(&quot;log: &quot;+buf)
  
  def on_connect(client, userdata, flags, rc):
      if rc==0:
          print(&quot;Connected OK&quot;)
          connection_status.config(text=&quot;Connection status: CONNECTED&quot;)
      else:
          print(&quot;Bad connection Returned code = &quot;, rc)
          connection_status.config(text=&quot;Connection status: BAD CONNECTION with error&quot; + rc)
  
  def on_disconnect(client, userdata, flags, rc=0):
      print(&quot;DisConnected result code &quot;+str(rc))
      connection_status.config(text=&quot;Connection status: DISCONNECTED&quot; + rc)
  
  def on_message(client, userdata, msg):
      topic=msg.topic
      global m_decode
      m_decode=str(msg.payload.decode(&quot;utf-8&quot;)+msg.topic)
      print(&quot;message received&quot;, m_decode)
      #testo_receive.configure(text=m_decode)
      testo_receive.config(text=&quot;Messaggio ricevuto: &quot; + (m_decode))
      
      #Implementazione del salvataggio di dati su file di testo.
      file = open(&quot;documento_test_diagnosi.txt&quot;, 'a')
      file.write(m_decode)
      file.write(&quot;\n&quot;)
      file.close()
  
  #Implementazione della funzione SEND, che sarà richiamata dal bottone che attiva questo servizio.
  def publish_message(client, msg_entry):
      msg=msg_entry.get()
      msg_entry.delete('0', 'end')
      
      client.publish(&quot;diagnostic_request/topic&quot;, msg)
      
  def cambia():
          testo_receive.configure(text=msg_entry.get())
  
  
  def resizer2(e):
      global bg3, resized_bg3, new_bg3
      bg3 = Image.open(&quot;WUT_banner_Automotive_1080x335.4532699.png&quot;)
      resized_bg3 = bg3.resize((e.width, e.height), Image.ANTIALIAS)
      new_bg3 = ImageTk.PhotoImage(resized_bg3)
      new_canvas1.create_image(0,0, image=new_bg3, anchor=&quot;nw&quot;)
  
  def open_window():
      global new_canvas1
      new_window = Tk()
      new_window.title(&quot;Diagnostic panel&quot;)
      new_window.geometry(&quot;700x400&quot;)
      bg = ImageTk.PhotoImage(file=&quot;WUT_banner_Automotive_1080x335.4532699.png&quot;)
      new_canvas1 = Canvas(new_window, width = 700, heigh =335)
      new_canvas1.pack(fill=&quot;both&quot;, expand=True)
      new_canvas1.create_image(0,0, image=bg, anchor=&quot;nw&quot;)
  
      #connection status
      global connection_status
      connection_status = Label(new_window, text=&quot;Connection status... &quot;, width=30, heigh=2, background=&quot;green&quot;, relief=&quot;raised&quot;)
      connection_status.place(x=80, y=100)
  
      #label in cui scrivere il messaggio diagnostico
      msg_entry = ttk.Entry(new_window, width=40).place(x=350, y=200)
      info_label = Label(new_window, text=&quot;Inserire una richiesta diagnostica: &quot;,  width=34, background=&quot;white&quot;,relief=&quot;groove&quot;).place(x=80, y=201)
  
      #Send button
      msg_button = ttk.Button(new_window, text=&quot;Send&quot;)
      msg_button.place(x=600, y=198)
      msg_button['command'] = lambda: publish_message(client, msg_entry)
      msg_button.bind('&lt;Return&gt;', lambda event: publish_message(client, msg_entry))
      
      
      #Receive window
      global testo_receive
      testo_receive = Label(new_window, text=&quot;In attesa di ricevere un messaggio... &quot;,  width=34, heigh=3, background=&quot;white&quot;,relief=&quot;groove&quot;).place(x=80, y=300)
     
      client.on_connect = on_connect
      client.ondisconnect=on_disconnect
      client.on_log = on_log
      client.on_message=on_message
      print(&quot;Connecting to broker &quot;, broker)
      client.connect(broker)
      client.loop_start()
      client.subscribe(&quot;diagnostic_response/topic&quot;)
      
      new_window.bind('&lt;Configure&gt;', resizer2)
      new_window.mainloop()
  
  #Funzione che adatta lo sfondo alle dimensioni della finestra (funzione collegata alla main window).
  def resizer(e):
      global bg1, resized_bg, new_bg
      bg1 = Image.open(&quot;WUT_banner_Automotive_1080x335.4532699.png&quot;)
      resized_bg = bg1.resize((e.width, e.height), Image.ANTIALIAS)
      new_bg = ImageTk.PhotoImage(resized_bg)
      my_canvas.create_image(0,0, image=new_bg, anchor=&quot;nw&quot;)
  
  #Funzione che adatta lo sfondo alle dimensioni della finestra (funzione collegata alla Services Window).  
  def resizer1(e):
      global bg2, resized_bg2, new_bg2
      bg2 = Image.open(&quot;WUT_banner_Automotive_1080x335.4532699.png&quot;)
      resized_bg2 = bg2.resize((e.width, e.height), Image.ANTIALIAS)
      new_bg2 = ImageTk.PhotoImage(resized_bg2)
      new_canvas.create_image(0,0, image=new_bg2, anchor=&quot;nw&quot;)
  
  def log_ok():
      global new_canvas
      user = e1.get()
      password = e2.get()
  
      if(user == &quot;&quot; and password == &quot;&quot;):
          messagebox.showinfo(&quot;&quot;, &quot;Inserisci le credenziali per accedere.&quot;)
  
      elif(user == &quot;admin&quot; and password == &quot;admin&quot;):
          messagebox.showinfo(&quot;&quot;, &quot;Login success.&quot;)
          root.destroy()
          #Se il login è ok, apri la finestra successiva.
          s = Tk()
          s.title(&quot;Services window&quot;)
          s.geometry(&quot;700x400&quot;)
          bg = ImageTk.PhotoImage(file=&quot;WUT_banner_Automotive_1080x335.4532699.png&quot;)
          new_canvas = Canvas(s, width = 700, heigh =335)
          new_canvas.pack(fill=&quot;both&quot;, expand=True)
          new_canvas.create_image(0,0, image=bg, anchor=&quot;nw&quot;)
  
          Button(s, text=&quot;Diagnostic request&quot;, command = lambda: [ s.destroy(),open_window() ] , heigh = 5, width = 15, bg = &quot;yellow&quot;).place(x=100, y=200)
  
          #Aggiungere bottoni per effettuare altre operazioni, come ACQUISIZIONE.
  
          s.bind('&lt;Configure&gt;', resizer1)
          s.mainloop()
  
  
  
  root = Tk()
  root.title(&quot;Home&quot;)
  root.geometry(&quot;700x400&quot;)
  
  #Operazioni per settare lo sfondo della schermata principale.
  bg = ImageTk.PhotoImage(file=&quot;WUT_banner_Automotive_1080x335.4532699.png&quot;)
  my_canvas = Canvas(root, width = 700, heigh =335)
  my_canvas.pack(fill=&quot;both&quot;, expand=True)
  my_canvas.create_image(0,0, image=bg, anchor=&quot;nw&quot;)
  
  #Aggiunta di loghi alla schermata Home
  photo = PhotoImage(file=&quot;logo_kienton.png&quot;)
  Label(root, image = photo).place(x=600, y=230)
  photo2 = PhotoImage(file=&quot;logo_dieti.png&quot;)
  Label(root, image = photo2).place(x=600, y=430)
  
  #Aggiunta dei parametri di logIN.
  global e1
  global e2
  
  Label(root, text = &quot;Username&quot;, width=20, heigh=2).place(x=80, y=300)
  Label(root, text = &quot;Password&quot;, width=20, heigh=2).place(x=80, y=420)
  e1 = Entry(root)
  e1.place(x=300, y=302, width=200, heigh=30)
  e2 = Entry(root)
  e2.place(x=300, y=422, width=200, heigh=30)
  e2.config(show=&quot;*&quot;)
  Button(root, text=&quot;Login&quot;, command = log_ok, heigh = 3, width = 13).place(x=80, y=520)
  
  
  root.bind('&lt;Configure&gt;', resizer)
  
  root.mainloop()
  </code></pre>
  <p>In particular I have a problem when I add the following lines for the button:</p>
  <pre><code>msg_button['command'] = lambda: publish_message(client, msg_entry)
      msg_button.bind('&lt;Return&gt;', lambda event: publish_message(client, msg_entry))
  </code></pre>
  <p>Without them is all ok.
  I want to clarify that I made run this cose in this form:</p>
  <pre><code>def open_window():
      new_window = Tk()
  
      msg_entry = ttk.Entry(new_window, width=40)
      msg_entry.grid(row=1, column=0)
      global testo_receive
      testo_receive = Label(new_window, text=&quot;In attesa di ricevere un messaggio... &quot;,  width=34, background=&quot;white&quot;,relief=&quot;groove&quot;)
      testo_receive.grid(row=2, column=0)
  
      global connection_status
      connection_status = Label(new_window, text=&quot;Connection status... &quot;, width=40, background=&quot;white&quot;, relief=&quot;raised&quot;, pady=5)
      connection_status.grid(row=7, column=0)
  
      msg_button = ttk.Button(new_window, text=&quot;Send&quot;)
      msg_button.grid(row=1, column=1)
      msg_button['command'] = lambda: publish_message(client, msg_entry)
      msg_button.bind('&lt;Return&gt;', lambda event: publish_message(client, msg_entry))
  
      q_button = ttk.Button(new_window, text=&quot;Quit&quot;)
      q_button.grid(row=1, column=2)
      q_button['command'] = lambda: new_window.close()
  
  
      client.on_connect = on_connect
      client.ondisconnect=on_disconnect
      client.on_log = on_log
      client.on_message=on_message
  
      print(&quot;Connecting to broker &quot;, broker)
  
      client.connect(broker)
      client.loop_start()
      client.subscribe(&quot;mangoh&quot;)
  
      new_window.mainloop()
  </code></pre>
  <p><strong>I had to change code because I want a background for my_windows that resizes with the window dimensions</strong>. I saw also other posts on this error:
  <a href="https://i.stack.imgur.com/zfG9R.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zfG9R.png" alt="enter image description here" /></a>
  But I can't understand where am I wrong. Can someone help me?
  Any help will be appreciated.</p>
  <p>Regards,
  Kevin</p>