Search code examples
pythonstringtextbluetoothtkinter-entry

Replacing a string with text from Entry box


So I have this generic code where a Bluetooth device is searched for. The device's name has to be specified through the python script. Down below I have my function script. I want "My Phone" to be manually typed in by the user.

class Bluetooth():

import Tkinter as tk

def connect(self):

    import bluetooth

    target_name = "My Phone"
    target_address = None

    nearby_devices = bluetooth.discover_devices()

    for bdaddr in nearby_devices:
        if target_name == bluetooth.lookup_name( bdaddr ):
            target_address = bdaddr
            break

    if target_address is not None:
        print "found target bluetooth device with address ", target_address
    else:
        print "could not find target bluetooth device nearby"

def retrieve_input(self):
    self.input1 = self.textfield.get()#("0.0",'END-1c')

def text_field(self):

    import Tkinter as tk

    self.textfield = tk.Entry()
    self.textfield.pack()
    #self.textfield.pack_forget()

Currently in my next script I have made 2 buttons. The first button creates the Entry text box. The second one should retrieve the text within Entry and then place that string as the target name above. This should replace "My Phone" but it hasn't worked yet.

import Tkinter as tk
from Tkinter import *
import database

root = Tk()
root.geometry("800x600+0+0")

bluetooth = database.Bluetooth()

button1 = tk.Button(height= 10, width= 10, command= bluetooth.text_field)#Entry
button1.pack()

def combine_funcs(*funcs):
        def combined_func(*args, **kwargs):
            for f in funcs:
                f(*args, **kwargs)
        return combined_func

button = tk.Button(height= 10, width=10, command = combine_funcs(bluetooth.retrieve_input, bluetooth.connect))#bluetooth test
button.pack()

root.mainloop()

--

connect.py", line 7, in connect
    import bluetooth
  File "C:\Python27\lib\site-packages\bluetooth\__init__.py", line 37, in <module>
    from msbt import *
  File "C:\Python27\lib\site-packages\bluetooth\msbt.py", line 2, in <module>
    import bluetooth._msbt as bt
ImportError: DLL load failed: %1 is not a valid Win32 application.

Solution

  • main.py

    import Tkinter as tk
    from Tkinter import *
    import database
    
    root = Tk()
    root.geometry("800x600+0+0")
    
    bluetooth = database.Bluetooth()
    
    button1 = tk.Button(text="Get Name", height=10, width=10, command=bluetooth.text_field)#Entry
    button1.pack()
    
    def my_function(entry, output):
        #print entry()
        output( entry() ) # bluetooth.connect( bluetooth.retrieve_input() ) 
    
    button = tk.Button(text="Connect", height=10, width=10, command=lambda:my_function(bluetooth.retrieve_input, bluetooth.connect))#bluetooth test
    button.pack()
    
    root.mainloop()
    

    database.py (see: retrieve_input() and connect(self, name="My Phone"))

    import Tkinter as tk
    import bluetooth
    
    class Bluetooth():
    
        def connect(self, name="My Phone"):
    
            target_name = name
            target_address = None
    
            nearby_devices = bluetooth.discover_devices()
    
            for bdaddr in nearby_devices:
                if target_name == bluetooth.lookup_name( bdaddr ):
                    target_address = bdaddr
                    break
    
            if target_address:
                print "found target bluetooth device with address ", target_address
            else:
                print "could not find target bluetooth device nearby"
    
        def retrieve_input(self):
            return self.textfield.get() #("0.0",'END-1c')
    
        def text_field(self):
            self.textfield = tk.Entry()
            self.textfield.pack()
            #self.textfield.pack_forget()