Python to Arduino serial communication

How to Set a variable and upload the sketch file to Arduino with a button click using python? I'm using Tkinter for the UI if it makes any difference.

Your question, in conjunction with your Title, is confusing.

Sending data to an Arduino program over a serial connection is not at all the same as uploading a new Arduino program. Which do you actually want to do?

If you just want to send data to an Arduino program then this Simple Python - Arduino demo should get you started.

If you actually want to use Python to upload a new Arduino program it will help to understand your requirement if you explain why you are not content to use the Arduino IDE.

...R

Well, I am making a head mouse for the physically disabled people. I wanted to make the project user friendly. So, I made a UI through which one can set the sensitivity of the mouse . To be clear, I want the program to be able to upload the arduino sketch after a button click from the UI and then wait for the user to enter the value of mouse sensitivity they want in the text box of the UI and thus setting the value of a mouse sensitivity variable in my code.

P.S:If my title is confusing, I will change the title as required. I learnt from stack overflow that these type of operations require serial communication.

Nahian_Alindo:
To be clear, I want the program to be able to upload the arduino sketch after a button click from the UI and then wait for the user to enter the value of mouse sensitivity they want in the text box of the UI and thus setting the value of a mouse sensitivity variable in my code.

This is still confusing. Serial communication is the correct term.

What is confusing is the combination of needing to upload the program and then update the sensitivity.

I don't understand why the program would not have been uploaded at some earlier time, and as a separate process from setting the sensitivity.

And you did not not explain why you can't just use the Arduino IDE to upload the program.

And taking all that together I am not convinced that you understand the difference between uploading a program and updating data that is used by an existing program. Consequently I don't know what advice to give.

As I envisage things you would write a program and upload it to an Arduino using the Arduino IDE. Then some time later (maybe hours, maybe days) you would give the programmed Arduino to somebody who would then use your Python program to set the sensitivity to suit themself.

...R

Yes, you're right. I couldn't make you understand properly. Your assumption is correct. I would program the Arduino with a default sensitivity and give to someone. But then I would give the user a UI to work with and set the mouse sensitivity according to his needs. But how would I do it? That is my question.

Nahian_Alindo:
But then I would give the user a UI to work with and set the mouse sensitivity according to his needs. But how would I do it? That is my question.

The link I gave you in Reply #1 illustrates the process of sending data to an Arduino. You may also wish to study Serial Input Basics - simple reliable ways to receive data.

The business of creating a GUI to make it easy for the user generate that data is outside the scope of this Forum. It can be done with most PC programming languages.

When the data has been received on the Arduino you should probably save it to the Arduino's EEPROM memory so that it is still available after the Arduino is reset.

...R