用Entry框中的文本替换字符串

时间:2014-06-16 21:09:42

标签: python string text bluetooth tkinter-entry

所以我有这个通用代码,搜索蓝牙设备。必须通过python脚本指定设备的名称。在下面我有我的功能脚本。我希望用户手动输入"My Phone"

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()

目前在我的下一个脚本中我制作了2个按钮。第一个按钮创建Entry文本框。第二个应检索Entry中的文本,然后将该字符串作为上面的目标名称。这应该取代"My Phone",但尚未奏效。

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.

1 个答案:

答案 0 :(得分:0)

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(请参阅:retrieve_input()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()