Python Forum
Sending string commands from Python to a bluetooth device
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending string commands from Python to a bluetooth device
#8
Here's the same code, but with individual functions associated with each button:
All you have to do is add the interface code:
import tkinter as tk


class BtnEngine:
    # rename class as you wish
    def __init__(self, parent):
        self.parent = parent
        self.parent.title("Tryout7")
        self.parent.minsize(100, 100)

        self.btn_row = 0
        self.btn_column = 0
        self.number_of_btns = 12
        self.btns_per_row = 4
        self.btn_number = 0
        self.btns = [None] * self.number_of_btns

        self.dispatch()

    def dispatch(self):
        self.create_buttons()
    
    def create_buttons(self):
        btn_titles = ["volume up", "volume down", "channel up", "channel down", 
            "conditioner on", "conditioner off", "raise the bed ", "lower the bed",
            "open television", "mute", "open curtain", "close curtain"]

        btn_actions = [self.volume_up, self.volume_down, self.channel_up, self.channel_down,
            self.conditioner_on, self.conditioner_off, self.raise_the_bed, self.lower_the_bed,
            self.open_television, self.mute, self.open_curtain, self.close_curtain]

        while self.btn_number < self.number_of_btns:
            if not ((self.btn_column + 1)  % self.btns_per_row):
                self.btn_row += 1
                self.btn_column = 0

            self.btns[self.btn_number] = (tk.Button(self.parent, text=btn_titles[self.btn_number],
                padx=60, pady=20, height = 2, width = 5,
                command=lambda idx=self.btn_number, btitle=btn_titles[self.btn_number], 
                bfunc=btn_actions[self.btn_number]: self.button_clicked(bfunc, btitle, idx)))

            self.btns[self.btn_number].grid(row=self.btn_row, column=self.btn_column)
            self.btn_column += 1
            self.btn_number += 1

    def button_clicked(self, fcntn, title, idx):
        fcntn(idx+1, title)

    def volume_up(self, idx, title):
        print(f"Button {idx} pressed, {title} command running")

    def volume_down(self, idx, title):
        print(f"Button {idx} pressed, {title} command running")

    def channel_up(self, idx, title):
        print(f"Button {idx} pressed, {title} command running")

    def channel_down(self, idx, title):
        print(f"Button {idx} pressed, {title} command running")

    def conditioner_on(self, idx, title):
        print(f"Button {idx} pressed, {title} command running")

    def conditioner_off(self, idx, title):
        print(f"Button {idx} pressed, {title} command running")

    def raise_the_bed(self, idx, title):
        print(f"Button {idx} pressed, {title} command running")

    def lower_the_bed(self, idx, title):
        print(f"Button {idx} pressed, {title} command running")

    def open_television(self, idx, title):
        print(f"Button {idx} pressed, {title} command running")

    def mute(self, idx, title):
        print(f"Button {idx} pressed, {title} command running")

    def open_curtain(self, idx, title):
        print(f"Button {idx} pressed, {title} command running")

    def close_curtain(self, idx, title):
        print(f"Button {idx} pressed, {title} command running")


def main():
    root = tk.Tk()
    BtnEngine(root)
    root.mainloop()


if __name__ == '__main__':
    main()
Reply


Messages In This Thread
RE: Sending string commands from Python to a bluetooth device - by Larz60+ - Aug-31-2021, 05:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending a text from Python sawtooth500 2 332 Apr-14-2024, 01:56 PM
Last Post: sawtooth500
  Read data via bluetooth frohr 9 3,725 Jul-10-2022, 09:51 AM
Last Post: frohr
  Connect to HC-05 Bluetooth (NOT BLE) korenron 0 1,550 Jun-26-2022, 09:06 AM
Last Post: korenron
  Read buffer from bluetooth frohr 2 2,252 Jun-01-2022, 01:31 PM
Last Post: frohr
  Scan for Bluetooth device korenron 0 2,734 Jan-10-2022, 01:06 PM
Last Post: korenron
  ModuleNotFoundError: No module named 'bluetooth' Error Rovelin 4 12,468 Aug-31-2021, 04:04 PM
Last Post: Rovelin
  Python library for win32 console commands eldiener 3 3,611 Aug-24-2021, 10:28 PM
Last Post: bowlofred
  Python BLE Scanner not detecting device alexanderDennisEnviro500 0 2,072 Aug-01-2021, 02:29 AM
Last Post: alexanderDennisEnviro500
  Possible to execute a python script before log off/shutdown with input commands? Kaltex 1 2,339 May-18-2021, 06:31 AM
Last Post: Skaperen
  Bluetooth send message after connecting? korenron 2 2,744 Apr-26-2021, 05:50 AM
Last Post: korenron

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020