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
#6
Here's how I would do the buttons:
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"]

        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, title = btn_titles[self.btn_number]: self.button_clicked(idx, title)))

            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, idx, title):
        print(f"Button {idx+1} named {title} pressed")
        # add code for specific button here


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


if __name__ == '__main__':
    main()
   
If you try this, here's what you get:
Output:
Button 1 named volume up pressed Button 2 named volume down pressed Button 5 named conditioner on pressed Button 12 named close curtain pressed
You can replace the print statements with the functions that you want to perform when the particular button is pressed.
You can even add the particular function you want to execute when the buttonis pressed (If you'd like to do this, I can show you how)
Rovelin likes this post
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending a text from Python sawtooth500 2 335 Apr-14-2024, 01:56 PM
Last Post: sawtooth500
  Read data via bluetooth frohr 9 3,733 Jul-10-2022, 09:51 AM
Last Post: frohr
  Connect to HC-05 Bluetooth (NOT BLE) korenron 0 1,553 Jun-26-2022, 09:06 AM
Last Post: korenron
  Read buffer from bluetooth frohr 2 2,256 Jun-01-2022, 01:31 PM
Last Post: frohr
  Scan for Bluetooth device korenron 0 2,737 Jan-10-2022, 01:06 PM
Last Post: korenron
  ModuleNotFoundError: No module named 'bluetooth' Error Rovelin 4 12,481 Aug-31-2021, 04:04 PM
Last Post: Rovelin
  Python library for win32 console commands eldiener 3 3,617 Aug-24-2021, 10:28 PM
Last Post: bowlofred
  Python BLE Scanner not detecting device alexanderDennisEnviro500 0 2,077 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,340 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