Python Forum
Display more than one button in GUI to display MPU6000 Sensor readings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Display more than one button in GUI to display MPU6000 Sensor readings
#3
Hi, As you mentioned I optimize the code accordingly but I am still not able to destroy the button after the click, I guess I am not placing the option correctly please check out my code :

import serial
import json
import tkinter
from tkinter import messagebox
from tkinter import *
import tkinter as ttk
import serial.tools.list_ports

root = ttk.Tk()
root.title("Read Sensor")

ard = serial.Serial('COM4', timeout=1); 

# Add a grid
mainframe = Frame(root)
mainframe.grid(column=0,row=0, sticky=(N,W,E,S) )
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)
mainframe.pack(pady = 100, padx = 100)
 
# Create a Tkinter variable
tkvar = StringVar(root)
 
# Dictionary with options
baud = { '9600','119200','34800'}
tkvar.set('9600') # set the default option

#Pop Up desciption
popupMenu = OptionMenu(mainframe, tkvar, *baud)
Label(mainframe, text="Baudrate").grid(row = 1, column = 1)
popupMenu.grid(row = 2, column =1)

#baudrate functions
def dropCall(*args):
   B = None
   value = tkvar.get()
   ## using a dictionary instead of if statements
   ## to show how dictionaries are used
   baud_dict={'9600':value, '34800':value, '119200':value}
   if value in baud_dict:
       ard.baudrate = int(baud_dict[value])
       if B:
           B.destroy()
       B = ttk.Button(root, text =value, command = helloCallBack)
       B.pack()   
    
#Link Function
tkvar.trace('w', dropCall)

    
#Serial Callback Functions
def helloCallBack():
    k = ard.readline().decode('ascii');
    if(len(k)>0):
        print (k);
        size = len(k);
        #print (k[0:size-2]);
        print (size);
        messagebox.showinfo('Message From Arduino',k[0:size-2]+'\n'+str(size))
Reply


Messages In This Thread
RE: Display more than one button in GUI to display MPU6000 Sensor readings - by jenkins43 - Jan-03-2019, 12:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] PyQt5 window closing when trying to display a graph bianca 4 1,745 Aug-12-2023, 03:25 PM
Last Post: bianca
  [PyQt] [solved] How to display a pdf-file in a PyQt6 widget BigMan 13 16,277 May-06-2023, 09:27 AM
Last Post: Axel_Erfurt
  [PyQt] [Solved]Display PyQtTable results from A->Z & Z->A Extra 2 1,175 Jul-18-2022, 04:04 PM
Last Post: Extra
  [PyQt] [Solved]Display Search Results in QTable Extra 5 2,479 Jun-29-2022, 10:20 PM
Last Post: Extra
  [PyQt] Cannot Display Image after Selecting Image bintangkecil 4 2,605 Jun-12-2022, 08:18 AM
Last Post: Axel_Erfurt
  looking for scripts that do simple image display Skaperen 10 4,297 Sep-13-2021, 05:35 PM
Last Post: FullOfHelp
  [PyQt] How do I display the DB table I will choose from the QComboBox in QTableWidget JokerSob 2 2,333 Aug-05-2021, 03:00 PM
Last Post: JokerSob
  Scaling text QLabel following display mode windows '100%, 125% ...) VIGNEAUD 2 2,273 Jul-07-2021, 06:38 PM
Last Post: deanhystad
  [Tkinter] How display matrix on canvas eedenj 9 9,037 Mar-17-2021, 03:58 PM
Last Post: eedenj
  [Tkinter] Display IP address in listbox dominicrsa 2 2,196 Oct-21-2020, 01:00 PM
Last Post: dominicrsa

Forum Jump:

User Panel Messages

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