Issues with DigiPot GUI slider with MAX 5487

Hi, I'm making a digipot with the MAX 5487 chip for my school project with touchscreen GUI using python. While I've gotten the resistance of the chip to change correctly, I've encountered some issues with the response time of the chip. Currently in the python code, if I decrease the timeout to anything below 1 second the chip will stop working. This leads to the chip responding very slowly to the GUI and the GUI being very laggy, operating at 1 fps. Is there anyway I can rectify this issue? I'm currently using an Arduino Mega 2560. Any help is greatly appreciated.

Here is the Python code:

import tkinter
from tkinter import *
import tkinter.font as tkFont
from PIL import ImageTk, Image
import serial
import time
import os

arduino = serial.Serial(port='COM3', baudrate=115200, timeout=1)

root = Tk()
root.config(bg='black')
root.title('final_code')
root.geometry("1920x1080")
def print_value(num):
    print (num)
    x = write_read(num)
def write_read(x):
    arduino.write(bytes(x, 'utf-8'))
    time.sleep(0.05)
    data = arduino.readline()
    return data
fontstyle = tkFont.Font(family='Cambria', size=50)

slide = tkinter.Scale(root,from_=0,to=255,length=700,bg='#F25278',fg='#0000FF', command=print_value)

slide.place(x=1300,y=100)
slide['font'] = fontstyle

slide.pack()
root.mainloop()

Here is the Arduino code:

 #include <SPI.h>

const byte ssPin = 53 ;
const byte powerPin = 45;
const byte directionPin = 47;
const byte modePin = 49;
const byte writepotA = B00000001;
const byte writepotB = B00000010;
const byte writeNVA = B00010001;
const byte writeNVB = B00010010;
int x;
String power, direction_, mode;

void setup() {

  Serial.begin(115200);
  pinMode(ssPin, OUTPUT);
  digitalWrite(ssPin, LOW);
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE3);

}

void loop() {
   while (! Serial.available());
   x = Serial.readString().toInt();
    setPotWiper(writepotA, x);
    setPotWiper(writepotB, x);
    delay(900);

}

void setPotWiper(int addr, int pos){

  pos =  constrain(pos, 0, 255);
  digitalWrite(ssPin, LOW);
  SPI.transfer(addr);
  SPI.transfer(pos);
  digitalWrite(ssPin, HIGH);

}

I'm not sure if your MAX 5487 is the culprit. Your use of Serial.readString() more than likely is as it uses a timeout.

To get some ideas how to handle serial communication at the Arduino side, you can have a look at Robin's Serial Input Basics - updated. Next you can adjust your Python code to match. Robin also wrote an accompanying Python article: Demo of PC-Arduino comms using Python.

If you insist on using String objects, try to use readStringUntil() and let your Python side send a terminator (e.g. '\n').