Dec-28-2018, 09:49 AM
Hi, I have been working with the serial monitoring program. My laptop connected to Arduino with USB port and trying to receive the MPU6000 I2C module data.
I have made the GUI in which I am selecting the items from drop-down list, After that a button is displayed and by clicking on that button I am able to display the message in a message box, and while selecting the different option from drop-down list then a new button appears on UI and the previous button is also available,
How Can I display only a single button while selecting the choice?
Here is my code
I have made the GUI in which I am selecting the items from drop-down list, After that a button is displayed and by clicking on that button I am able to display the message in a message box, and while selecting the different option from drop-down list then a new button appears on UI and the previous button is also available,
How Can I display only a single button while selecting the choice?
Here is my code
import serial
import json
import tkinter
from tkinter import messagebox
from tkinter import *
import tkinter as ttk
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):
value = tkvar.get()
if value == '9600':
key_96(value)
elif value == '119200':
key_119(value)
elif value == '38400':
key_348(value)
else:
key_96(value)
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))
#Key Functions
def key_96(val1):
v1 = int(val1)
ard.baudrate = v1
print(v1)
def key_119(val2):
v2 = int(val2)
ard.baudrate = v2
print(v2)
def key_348(val3):
v3 = int(val3)
ard.baudrate = v3
print(v3)
