title: Bluetooth library and Tkinter not playing nice
body: <p>I am using the Python bluetooth module with Tkinter to create an app where users can choose to connect to Bluetooth. Selecting the Bluetooth button should trigger a buffer screen, which scans for BT names and addresses, and then displays the menu where the user can select what Bluetooth device they can connect to. Currently it is just a UI prototype and cannot connect to Bluetooth, only read the addresses. The problem is that, no matter where I put this Bluetooth code, it is executing before it is called, causing Tkinter (or Python) to skip the first page and go straight to the buffer. In other words, it is acting like the user selected the button already. I've had this problem before, and cannot work around it any more. Here is the code.</p>
  <pre><code>#start.py
  
  from tkinter import *
  import tkinter.font as font 
  
  import bt_buff
  import usb_buff
  
  ############################################################
  
  #Applied to button
  font_A=font.Font(weight=&quot;bold&quot;, size=50)
  
  #Creating window
  window.title(&quot;Testbench&quot;)
  window.geometry('500x500')
  window.grid_rowconfigure(0, weight=1)
  window.grid_columnconfigure(0, weight=1)
  window.resizable(True,True)
  
  #Shortcut for window close
  def kill():
      window.destroy()
  
  def bt_call():
      kill()
      bt_buff.go()
  
  def usb_call():
      kill()
      usb_buff.go()
  
  #Rendering instruction as label
  label=Label(window,text=&quot;Select your connection&quot;, font=('Helvetica bold', 40))
  label.grid(column=0, row=0)
  label.grid_rowconfigure(1, weight=1)
  label.grid_columnconfigure(1, weight=1)
  
  #Creating continue button
  next_B = Button(window, text=&quot;Bluetooth&quot;, width=40, bg='grey', command=bt_call)
  next_B['font']=font_A
  next_B.grid(column=0, row=1)
  
  #Creating continue button
  next_U = Button(window, text=&quot;USB&quot;, width=40, bg='grey', command=usb_call)
  next_U['font']=font_A
  next_U.grid(column=0, row=1)
  
  window.mainloop()
  </code></pre>
  <pre><code>#bt_buff.py
  
  from tkinter import *    
  from PIL import ImageTk,Image 
  import tkinter.font as font 
  import menu
  import uni
  import bluetooth
  ###########################################################
  
  #Holders for Bluetooth values
  address=[]
  names=[]
  
  #Shortcut for window close
  def kill():
      window.destroy()
  
  #Opens next window
  def nxt():
      uni.mute()
      kill()
      menu.go(names, address)
  
  #Create window
  window = Tk()
  window.title(&quot;Loading...&quot;)
  window.geometry('500x500')
  
  #Bluetooth search protocol (pulls addresses and device names)
  nearby_devices=bluetooth.discover_devices(lookup_names=True)
  
  for addr,name in nearby_devices:
      address.append(addr)
      names.append(name)
  
  nxt()
  </code></pre>
  <pre><code>#menu.py
  
  import bluetooth 
  import testBench
  import uni
  from tkinter import *
  import tkinter.font as font
  from tkinter import messagebox
  ###########################################################
  
  #Hacky fix to make the window a global variable for case-specific closing (kill())
  window = Tk()
  
  #Shortcut for window close
  def kill():
      window.destroy()
  
  #Main call, called from start.py
  def go(names,address):
  
      #Applied to button
      font_A=font.Font(weight=&quot;bold&quot;, size=50)
  
      #Creating window
      window.title(&quot;Connecting...&quot;)
      window.geometry('500x500')
      window.grid_rowconfigure(0, weight=1)
      window.grid_columnconfigure(0, weight=1)
      window.resizable(True,True)
  
      #Error message for missing feature
      def hold():
          messagebox.showinfo(&quot;Error 999&quot;,&quot;This widget doesn't have the ability to connect to Bluetooth yet... If you want to proceed, please click 'Ok' on the main screen.&quot;)
  
      #Opens next window
      def nxt():
          kill()
          testBench.go()
          
      #Rendering instruction as label
      label=Label(window,text=&quot;Pick either an address or name to connect to&quot;, font=('Helvetica bold', 40))
      label.grid(column=0, row=0)
      label.grid_rowconfigure(1, weight=1)
      label.grid_columnconfigure(1, weight=1)
  
      #Creating continue button
      next_B = Button(window, text=&quot;Ok&quot;, width=40, bg='grey', command=nxt)
      next_B['font']=font_A
      next_B.grid(column=0, row=1)
  
      #Create a Menu Bar
      menubar= Menu()
      name_menu= Menu(menubar, tearoff=False)
      addr_menu= Menu(menubar, tearoff=False)
      menubar.add_cascade(label= &quot;Name&quot;,underline=0, menu= name_menu)
      menubar.add_cascade(label= &quot;Address&quot;,underline=0, menu= addr_menu)
  
      #Populating Menu with found Bluetooth data
      for x in names:
          name_menu.add_command(label=x, underline= 1, command=hold)
  
      for y in address:
          addr_menu.add_command(label=y, underline= 1, command=hold)
  
      window.config(menu= menubar)
  
      window.mainloop()
  </code></pre>