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